而不是两次要求inpu

2024-05-18 11:17:09 发布

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

scenario2(outcome)函数调用sget\u choice3 one,以便根据用户的选择打印结果。你知道吗

如果用户选择为0,我希望函数循环并请求输入两次(正确的选择是1,0只是它的一个分支)。你知道吗

s2option_list3 =['Try to force it', 'Examine the door']

    def sget_choice3(s2option_list3):
        i = -1
        while i <= 0:
            print s2option_list3
            choice3 = raw_input("> ")
            for i, opt in enumerate(s2option_list3):
                if opt in choice3:
                    i = i + 1
                    return i 
                else:
                    i = i + 2
                    return i             

            print "You need to select one of the given actions"

def scenario2_exit(outcome): 
    print outcome
    choice = outcomes[0]

    if outcome in choice:
        print "conversation"

        res3 = sget_choice3(s2option_list3)

        if res3 == 0:
            print "You try to force it but without any luck. However you do notice a little button on the left corner. "

        else:
            print "A small panel comes out, seems to be requesting a code. " 
            print "conversation"            

    else:
        print "conversation"

Tags: theto用户inifoneelseprint
1条回答
网友
1楼 · 发布于 2024-05-18 11:17:09

您的问题似乎在这一行:

for i, opt in enumerate(s2option_list3):

当您像那样使用enumerate时,它会用枚举的索引覆盖i-1的值(第一个选项是0,第二个选项是1)。我不知道你为什么要使用枚举。你知道吗

在不给你太多关于如何重组你的程序的建议的情况下,一个简单的解决方案可以是:

for opt in s2option_list3:

相关问题 更多 >