Python标记错误?

时间:2016-06-20 13:46:06

标签: python

我对Python很陌生。在学校,我们必须创建一个数学游戏,我正在创建一个9倍的桌面游戏。但是,即使它是正确的,它告诉我答案是不正确的?请帮忙!

import random
print(" Hello there!")#prints hello there
print("What is your name?")#asks user for a name
name= input()#sets name to the input
print(name, ", are you ready to test your nine times table? ")#asks user if they are ready to test their 9 times table
print (name, ", here we go!")#prints here we go
print ("Question 1:")#prints Question 1:
number1 = random.randint(1,12)#sets number1 to a random value from 1 to 12
answer= 9*number1#sets answer to 9 times number1
print ("what is 9 times", number1, "?")#asks what is 9 times number 1
if answer==input():
    print ("well done," , name,"thats correct!")
else:
    print("sorry, the correct answer was", answer, )

1 个答案:

答案 0 :(得分:0)

您的问题是您要将字符串(input())与数字(answer)进行比较。

您可以通过执行以下操作来解决此问题:

if answer==int(input()):

这会将您的input()转换为int。作为一个好处,如果用户输入int以外的其他内容,它也会引发错误。

这假设您将继续进行整数乘法运算。如果没有,您可能需要查看float()