【发布时间】:2011-11-19 19:56:07
【问题描述】:
我正在尝试找出循环这个简单数学测验程序的最佳方法(这里最好的意思是最简洁和最简单的方法)。我得到两个随机数及其总和,提示用户输入并评估该输入。理想情况下,当他们想再次玩游戏时,它应该获得新号码,并在提示不是有效答案时提出相同的问题......但我似乎无法思考如何去做。
import random
from sys import exit
add1 = random.randint(1, 10)
add2 = random.randint(1, 10)
answer = str(add1 + add2)
question = "What is %d + %d?" % (add1, add2)
print question
print answer
userIn = raw_input("> ")
if userIn.isdigit() == False:
print "Type a number!"
#then I want it to ask the same question and prompt for an answer.
elif userIn == answer:
print "AWESOME"
else:
print "Sorry, that's incorrect!"
print "Play again? y/n"
again = raw_input("> ")
if again == "y":
pass
#play the game again
else:
exit(0)
【问题讨论】:
-
这听起来像是家庭作业。我正在添加作业标签;如果不是作业,你可以删除它。
标签: python loops user-input