Home > Net >  creating a math quiz using if else [duplicate]
creating a math quiz using if else [duplicate]

Time:09-24

Hi i'm using python to write a simple math quiz for my college class using if else statements but my code will only put out the (else) print even when the (if) conditions are met.

Code Below

import random
num1 = random.randint(1,20)
num2 = random.randint(1,20)
print(num1,' ',num2)
user_answer = input("Your answer here: ")
real_answer = num1 num2
print(real_answer, user_answer)
if user_answer == real_answer:
    print("Yes")
else:
    print("no")

CodePudding user response:

Assuming that you're using Python 3.x (and not 2.x), your problem is that input returns a string, not a number. You need to explicitly convert it to one.

user_answer = int(input("Your answer here: "))

CodePudding user response:

You're testing a string against an int. Try something like:

if int(user_answer) == real_answer:
    print("Yes")
else:
    print("no")

CodePudding user response:

I think the types are different you have to convert input to integer first like below.

import random
num1 = random.randint(1,20)
num2 = random.randint(1,20)
print(num1,' ',num2)
user_answer = int(input("Your answer here: "))
real_answer = num1 num2
print(real_answer, user_answer)
if user_answer == real_answer:
    print("Yes")
else:
    print("no")
  • Related