কম্পিউটার টিউটোরিয়াল

বেসিক পাইথন প্রোগ্রামিং চ্যালেঞ্জ: আর্থমেটিক কুইজ গেম

এই টিউটোরিয়ালে আমরা একটি চ্যালেঞ্জের সমাধান লিখব: র‍্যান্ডম আর্থমেটিক প্রশ্ন তৈরি করে ইউজারের উত্তর যাচাই এবং স্কোর দেখানো।

চ্যালেঞ্জ

ইউজার প্রশ্নের সংখ্যা দেবে, প্রোগ্রাম র‍্যান্ডম আর্থমেটিক প্রশ্ন জেনারেট করবে। প্রতিটি প্রশ্নের উত্তর দিতে হবে। শেষে টোটাল স্কোর দেখাবে।

উদাহরণ কোড

import random
import operator

def main(n):
    print("Welcome to the quiz\nYou should answer floats upto 2 decimals")
    score = 0
    
    for i in range(n):
        is_correct, answer = question(i)
        if is_correct:
            score += 1
            print('Correct Congrats!')
        else:
            print(f'Incorrect! Answer: {answer}')
    
    print(f'Total score: {score}')

def question(n):
    answer = generate_question()
    user_answer = float(input("Answer: "))
    return user_answer == answer, answer

def generate_question():
    operators = {
        '+' : operator.add,
        '-' : operator.sub,
        '*' : operator.mul,
        '/' : operator.truediv,
        '//' : operator.floordiv,
        '%' : operator.mod
    }
    nums = [i for i in range(10)]
    _1, _2 = nums[random.randint(0, 9)], nums[random.randint(0, 9)]
    symbol = list(operators.keys())[random.randint(0, 5)]
    answer = round(operators.get(symbol)(_1, _2), 2)
    print(f'{_1} {symbol} _2?')
    return answer

if __name__ == '__main__':
    main(5)

আউটপুট (নমুনা)

Welcome to the quiz
You should answer floats upto 2 decimals
5 + 7? Answer: 12
Correct Congrats!
9 / 9? Answer: 1
Correct Congrats!
4 + 7? Answer: 11
Correct Congrats!
6 // 6? Answer: 1.0
Correct Congrats!
9 % 3? Answer: 0
Correct Congrats!
Total score: 5

উপসংহার

আপনি ডিফিকাল্টি বাড়াতে পারেন, ইزی থেকে হার্ড কুইস্ট্যাশন জেনারেট করতে পারেন। নিজে চেষ্টা করুন। উপভোগ করুন! প্রশ্ন থাকলে কমেন্ট করুন।


  1. পাইথনে Tkinter প্রোগ্রামিং

  2. পাইথন প্রোগ্রামিং-এ ইমেজ ভিত্তিক স্টেগানোগ্রাফি

  3. উইন্ডোজে পাইথন প্রোগ্রামিং-এ IDE সমূহ

  4. পাইথনে Turtle গ্রাফিক্স: আঙ্কন ও অ্যানিমেশন