带中断的python嵌套循环

带中断的python嵌套循环,python,while-loop,nested-loops,Python,While Loop,Nested Loops,嗯,我在学习python,我在尝试制作这种文本游戏,我被卡住了 在while循环中…我试图做的是列出可以使用的东西,并将用户的原始输入与此列表进行比较,如果他们在5次尝试中选择了正确的输入,则继续,否则将随消息一起消失。 这是我的密码: def die(why): print why exit(0) #this is the list user's input is compared to tools = ["paper", "gas", "lighter", "glass",

嗯,我在学习python,我在尝试制作这种文本游戏,我被卡住了 在while循环中…我试图做的是列出可以使用的东西,并将用户的原始输入与此列表进行比较,如果他们在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
def模具(为什么):
打印原因
出口(0)
#这是与用户输入进行比较的列表
工具=[“纸张”、“气体”、“打火机”、“玻璃”、“燃料”]
#用户输入附加到的空列表
yrs=[]
选择=无
打印“你需要生火”
虽然选择不在工具中:
打印“输入您将使用的内容:”
选择=原始输入(“>”)
yrs.append(选项)
而yrs<5:
印刷年鉴
死(“你试了太多次”)
如果选择工具:
打印“做得好,%s是您需要的选择”
打破
但选择并没有被添加到列表
yrs
,它只使用一个while循环 但它会一直持续下去,或者直到工具列表中的一项被输入为用户输入, 但是,我想将其限制为5次尝试,然后输入:
die(“您尝试了太多次”)
但在第一次尝试后,它给了我死亡的信息。。。 我在搜索这个论坛,没有找到满意的答案,请帮我试试

if len(yrs) < 5: 
   print yrs
else:
   die("you tried many times")
这也是不正确的,因为条件
len(yrs)<5
在第一次检查时将计算为
True
,因此最终将进入无限循环,用户将无法提供任何输入,这取决于条件
len(yrs)<5
的长度

您需要在
if
语句(如上所述)中将
yrs
的长度与5进行比较,以查看用户的尝试是否超过5次。如果它们不超过5,则在重复外部
while
循环之前,代码流应继续进行最终检查(
如果在工具中选择)。
尝试

if len(yrs) < 5: 
   print yrs
else:
   die("you tried many times")
    from sys import exit

    def die(why):
        print why
        exit()

    tools = ["paper", "gas", "lighter", "glass", "fuel"]
    choice = ''
    tries = 0  

    print 'You have to make fire'

    while choice not in tools:
        choice = raw_input('What do you want to do?-->')
        tries += 1
        if tries == 5:
            die('You tried too many times')

     print 'Well done you made a fire!'
这也是不正确的,因为条件
len(yrs)<5
在第一次检查时将计算为
True
,因此最终将进入无限循环,用户将无法提供任何输入,这取决于条件
len(yrs)<5
的长度


您需要在
if
语句(如上所述)中将
yrs
的长度与5进行比较,以查看用户的尝试是否超过5次。如果它们不超过5,则在重复外部
while
循环之前,代码流应继续进行最终检查(
If choice in tools
..),以允许用户再次尝试。

这是无效的Python语法。修复代码的缩进。这不是有效的Python语法。修复代码的缩进。
    from sys import exit

    def die(why):
        print why
        exit()

    tools = ["paper", "gas", "lighter", "glass", "fuel"]
    choice = ''
    tries = 0  

    print 'You have to make fire'

    while choice not in tools:
        choice = raw_input('What do you want to do?-->')
        tries += 1
        if tries == 5:
            die('You tried too many times')

     print 'Well done you made a fire!'