-3

I'm making a maths test, where if the user puts the correct input i want it to print correct.I have this

answer = (input) ("What is your answer?")
maths = eval(str(num1) + operation + str(num2))


if answer == maths:
    print ('Correct')

else:
    print('Wrong')

However its not working. It doesn't print correct, it just prints wrong whether input is correct or not. cheers aron

1 Answers1

0

input() returns a string value, not an integer. You cannot compare integers and strings and expect them to be equal:

>>> '42' == 42
False

Convert your input to an integer first:

answer = input("What is your answer?")
answer = int(answer)

This can lead to a ValueError exception if the input was not numeric however. See Asking the user for input until they give a valid response for options on how to handle that possibility.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343