同时不断要求输入。没有跳到第二个功能

时间:2014-12-25 23:15:02

标签: python

此代码,更具体地说是第28行,要求用户进行多种选择。代码确实有效,但有一个问题,我找不到适应我的情况的答案(这不会因为某种原因阻止你们中的某些人贬低这个问题),不管怎么说,事情是它一直要求输入即使我告诉它跳到第二个函数(scenario_exit),我没有复制到这里以保持简单。这就是问题所在,即使我告诉他们停下来,也一直要求我输入。

所以,太久没读了?代码循环在"您需要选择一个选项",第40行。

scenario2options =['Approach exit door', 'Check for vital signs']
scenario2options_2 =['Shoot him', 'Let him live']

def scenario2(response):
    print response
    print "Conversation"
    print "Conversation2"
    print "Conversation3"
    print scenario2options

    decision_sc2 = 1

    while decision_sc2 == 1:
        decision_scenario2 = raw_input("> ")

        if 'Approach exit door' in decision_scenario2:
            print "Conversation" 
            decision_sc2 = 0
            dead("Conversation")
        elif 'Check for vital signs' in decision_scenario2:
            print "One of them is still alive finish him off, says Lars"


            print scenario2options_2

            decision_sc2_2 = 1

            while decision_sc2_2 == 1:

                decision2_scenario2 = raw_input("> ")

                if 'Shoot him' in decision2_scenario2:                     
                    decision_sc2_2 = 0
                    scenario2_exit("You deliver him a quick dead.")
                elif 'Let him live' in decision2_scenario2:                   
                    decision_sc2_2 = 0
                    scenario2_exit("Conversation")

                else:
                    print "You need to select one option"
                    decision_sc2_2 = 1

        else:
            print "You need to select one option"  

2 个答案:

答案 0 :(得分:2)

'Check for vital signs' if块中,您没有将decision_sc2设置为任何不同的内容,因此您的外部循环仍会重复。

考虑使用这样的函数来简化这些事情:

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

然后你可以检查:

if get_choice(scenario2options)==0: # first choice, i.e. Approach exit door
    ...
else: # other choice, i.e. Check for vital signs
    ...

不需要连续的嵌套while循环。

答案 1 :(得分:1)

你的外部while循环将始终只评估else分支。因此,即使输入decision_sc2_2(你需要处理你的变量命名....)是0(不需要在输入的最后一个else子句中一直将它重置为1),它将循环通过在外面再次,将decision_sc2_2重置为1,并且“游戏”继续。