为什么正确的答案出错了

时间:2015-04-26 10:48:55

标签: python

{{1}}

什么是1次5?五 不,我担心答案是5。

9次3? 27 不,我担心答案是27。

4次1? 4 不,我担心答案是4。

2 个答案:

答案 0 :(得分:2)

您必须将输入字符串转换为数字:

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

    ans = int(input("What's %d times %d? " % (n1, n2)))
    if ans == prod:
        print("That's right -- well done.\n")
        correct += 1
    else:
        print("No, I'm afraid the answer is %d.\n" % prod)
print("\nI asked you 10 questions. You got %d of them right." % correct)
print("Well done!") 

答案 1 :(得分:0)

在您的情况下,ans是字符串。所以,你应该像:

ans = int(input("What's %d times %d? " % (n1, n2)))