【发布时间】:2015-03-12 14:37:26
【问题描述】:
我正在学习 python,我正在尝试制作这种文本游戏,但我被卡住了 在 while 循环中......我想做的是列出可以使用的东西,并将用户的 raw_input 与该列表进行比较,如果他们在 5 次尝试中选择了正确的,则继续,否则会因消息而死。 这是我的代码:
def die(why):
print why
exit(0)
#this is the list user's input is compared to
tools = ["paper", "gas", "lighter", "glass", "fuel"]
#empty list that users input is appended to
yrs = []
choice = None
print "You need to make fire"
while choice not in tools:
print "Enter what you would use:"
choice = raw_input("> ")
yrs.append(choice)
while yrs < 5:
print yrs
die("you tried too many times")
if choice in tools:
print "Well done, %s was what you needeed" % choice
break
但是选择没有被添加到列表yrs,它只适用于一个while循环
但是它会永远消失,或者直到工具列表中的一项作为用户输入输入,
但是我喜欢将其限制为 5 次尝试,然后输入:die("You tried too many times")
但它在第一次尝试后立即给了我死亡信息......
我正在搜索这个论坛,没有找到满意的答案,请帮助我
【问题讨论】:
-
这不是有效的 Python 语法。修正代码的缩进。
标签: python while-loop nested-loops