Reputation: 23
I am trying make a maths game but the output says wrong when I answer it correctly. (on python 2.7.2) The code:
import random
number1 = random.randrange(1, 10)
number2 = random.randrange(1, 10)
print "Whats",number1,"+",number2
answer = raw_input('=')
if answer is (number1 + number2):
print "Correct!"
else:
print "Wrong!"
Upvotes: 0
Views: 48
Reputation: 8786
You want to use ==
instead of is
. is
is used for checking reference equality. You can look at this for a better explanation. raw_input
reads values in as strings by default, so you need to convert it to an integer to be able to check if the input is equal to the answer. Or more simply, use input
instead.
import random
number1 = random.randrange(1, 10)
number2 = random.randrange(1, 10)
print "Whats",number1,"+",number2
answer = input('=')
if answer == (number1 + number2):
print "Correct!"
else:
print "Wrong!"
Upvotes: 3