我不能让这个简单的数学测验工作。答案总是错的

时间:2014-11-16 12:46:59

标签: python-3.x

from random import randint

stats = {'correct': 0, 'wrong': 0}
correct = 0

for i in range(10):
    n1 = randint(1, 10)
    n2 = randint(1, 10)
    prod = n1 * n2

    ans = input("What's %dx%d?" % (n1, n2))
    if ans == prod:
       print ('Your answer is correct. Well done.')
       stats['correct'] += 1
    else:
        print (('Your answer is wrong. I am afraid the answer is %d.') % prod)
        stats['wrong'] += 1

print (("\nI asked you 10 questions. You got %d of them right.") % correct)
print ("Well done!")

1 个答案:

答案 0 :(得分:0)

从输入中收到的答案是一个字符串。

    if ans == str(prod):

应该做的诀窍......