【发布时间】:2016-06-20 13:46:05
【问题描述】:
我对 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, )
【问题讨论】:
-
如果您使用的是 Python 3(看起来很像),这个
if answer==input():正在将字符串与数字进行比较。 -
print(" Hello there!")#prints hello there不要这样做!注释是为了解释程序的不明显之处,而不是为了大声朗读。这种噪音只会让您的代码更难阅读和使用。
标签: python