Skip to content Skip to sidebar Skip to footer

Why Does My Math Quiz Always Print Incorrect When The Answer Is Correct

okay so im writing a code that randomly generates questions and lets the user answer but my problem is that even if the user gets the answer right it will always print incorrect pr

Solution 1:

You need to compare guess with answer.

    print ("what is your username")
    name = input().title()
    print (name, "welcome")
    import random
    score=0
    question=0
    for i in range(10):
        ops = ["+", "-", "*"]
        num1 = random.randint (0,10)
        num2 = random.randint (0,10)
        oparator = random.choice(ops)
        Q=(str(num1)+(oparator)+(str(num2)))
        print (Q)
        guess = input()
        guess = int(guess)
        if oparator =='+':
            answer = int(str(num1+num2)) # Convert to int

        elif oparator =='-':
            answer = int(str(num1-num2))

        else:
            oparator =='*'
            answer = int(str(num1*num2))

        if guess == answer:  # Compare user's answer with actual answer
            print ("correct")
            score = score + 1 # Update the score

        else:
            print ("incorrect") 

Post a Comment for "Why Does My Math Quiz Always Print Incorrect When The Answer Is Correct"