简体   繁体   English

Python 3.X跳到一行代码

[英]Python 3.X jumping to a line of code

I'm trying to make a Choose Your Own Adventure game in Python 3.X. 我正在尝试使用Python 3.X创建一个“选择自己的冒险”游戏。 I'm using the idle platform. 我正在使用空闲平台。 Here is a sample of my code. 这是我的代码示例。

import time
again = True  
while again == True:  
    print("You wake up as you normally do, but something's not quite right. What will be the first thing you do? \n Choices: \n Go back to sleep \n Get up")  
    action1 = input(": ")  
    if action1 == "Go back to sleep":  
        print("You lazyhead. As you were about to fall asleep, your ceiling collapsed on you and killed you. Don't be lazy.")  
    playAgain = input("Play again? Y/N: ")  
    if playAgain == "Y":  
        again = True  
    elif playAgain == "N":  
        again = False   

Obviously, there is stuff between action1 and playAgain . 显然,在action1playAgain之间有东西。 I want to replace again = false in elif playAgain =="N" 我想在elif playAgain =="N" again = false替换again = false

elif playAgain =="N":
    again = false

I want to instead jump to playAgain so I don't just end the program. 我想跳playAgain再次playAgain所以我不只是结束程序。 This is my first question so my formatting is probably a bit weird so please correct that too. 这是我的第一个问题,因此我的格式可能有点怪异,因此也请更正。

As mentioned, python doesn't support GoTo . 如前所述,python不支持GoTo One way of achieving the same result is to create a quit game function like below then call it at the end of your main game code. 实现相同结果的一种方法是创建一个退出游戏函数,如下所示,然后在主游戏代码的末尾调用它。

def quit_game():
    playAgain = input("Play again? Y/N: ")  
    if playAgain == "Y":  
        return True
    elif playAgain == "N":  
        quit_game() 

In original code: 用原始代码:

...
again = quit_game()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM