如何使我的python代码循环回来

时间:2016-04-02 18:38:10

标签: python loops if-statement while-loop

我是python的新手,我正试图通过制作一个简单的游戏来学习。我希望用户能够输入他们的动作,然后如果他们选择不再继续,他们将被再次发送回输入阶段。我尽力尝试这一点,但一旦他们选择不“继续”,他们就再也无法选择我提出的任何其他选择了。

非常感谢帮助。

print ("How do you proceed?")
print ("Do you Shout? Look? Continue?")
action1 = input()

if action1 == ("Continue"): #Continue to next section
    print ("You continue on throught the darkness untill you reach a cold stone wall")
    pass

elif action1 == "Shout":
    print ("In the dark noone can hear you scream.")

elif action1 == ('Look'):
    print ("'I cannae see anything in the dark' you think to yourself in a braod Scottish accent.")

while action1 != ("Continue"):
    print ("Enter your next action.(Case sensitive!!)")
    print ("Shout? Look? Continue?")
    action1 = input() #Want this too loop back to start of action menu

1 个答案:

答案 0 :(得分:0)

如果用户输入有效,您可以将整个过程放入一个while循环并跳出循环:

print("How do you proceed?") 

while True:          
    print("Do you Shout? Look? Continue?")
    action1 = input()

    if action1 == "Continue": # Continue to next section
        print("You continue on throught the darkness untill you reach a cold stone wall")
        break # break out of the while loop

    elif action1 == "Shout":
        print("In the dark none can hear you scream.")

    elif action1 == 'Look':
        print("'I cannot see anything in the dark' you think to yourself in a broad Scottish accent.")

    print("Enter your next action.(Case sensitive!!)")

# You'll land here after the break statement