I am trying to make a multiplication game for my sister, but my code doesn't go through if-else statements properly. I have no idea why this is so. Can someone tell what I am doing wrong?
from random import randint
print "Welcome to Multiplication Practice."
print "\n-------"
correct = 0
wrong = 0
while wrong<3:
a = randint(1,9)
b = randint(1,9)
print "What is %s x %s?" %(a,b)
c = a * b
action = raw_input("> ")
if action == c:
print "correct!"
correct +=1
elif action != c:
print "Wrong!"
wrong +=1
else:
print "Invalid answer."
print correct
print wrong
raw_input
returns a string. This will never compare equal to a number. You need to convert the entered string to a number:
action = int(raw_input("> "))
raw_input
始终返回一个字符串,您需要将其转换为要与之比较的类型,在这种情况下,它是int(raw_input("> "))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.