【问题标题】:How to make my python code loop back如何让我的python代码循环回来
【发布时间】:2016-04-02 18:38:09
【问题描述】:

我对 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

【问题讨论】:

    标签: python loops if-statement while-loop


    【解决方案1】:

    您可以将整个过程放入一个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
    

    【讨论】:

      猜你喜欢
      • 2017-07-20
      • 1970-01-01
      • 2017-03-09
      • 2022-11-25
      • 1970-01-01
      • 2016-05-08
      • 1970-01-01
      • 2019-02-10
      • 2022-12-17
      相关资源
      最近更新 更多