While True功能的表现与预期不符。要求Twilight输入。

2024-05-17 22:55:53 发布

您现在位置:Python中文网/ 问答频道 /正文

此函数的目的是检查用户的输入,确定其是否在列表中,如果不在列表中,则告诉用户“选择给定选项之一”。你知道吗

问题是它两次要求我输入。当我选择“接近出口门”选项时,它会再次循环并请求输入,当我再次输入相同的选项时,也就是当我得到结果时(这导致dead()函数没有复制到这里以保持简短)。尝试了几种方法,比如添加一个关闭循环的条件,并试图使While为False,这样它就停止了,但是没有起作用。另外,感谢用户khelwood为我提供了这个替代方案并简化了我的程序。你知道吗

太久没读了?循环要求输入两次,无法修复。

这是密码。你知道吗

s2option_list =['Approach exit door', 'Check for vital signs']

def sget_choice(s2option_list):
    while True:
        print s2option_list
        choice = raw_input('> ')
        for i, opt in enumerate(s2option_list):
            if opt in choice:
                 return i
            else:
                print "You need to select one of the given options."

def scenario2(response):
    print response
    print "Conversation"
    print "Conversation"
    print "Conversation"

    sget_choice(s2option_list)

    if sget_choice(s2option_list)==0:
        dead("Conversation")              
    else:
        print "Conversation"

        sget_choice2(s2option_list2)   

Tags: 函数用户in列表fordef选项list
1条回答
网友
1楼 · 发布于 2024-05-17 22:55:53
def scenario2(response):
    print response
    print "Conversation"
    print "Conversation"
    print "Conversation"    
    res = sget_choice(s2option_list) # call it once save return value in a variable   
    if res == 0: # now you are checking the value from the previous call not calling again
        dead("Conversation")              
    else:
        print "Conversation"   
        sget_choice2(s2option_list2) 

相关问题 更多 >