Python 3.X跳转到一行代码

时间:2016-06-25 22:06:43

标签: python python-3.x

我正在尝试用Python 3.X制作一个选择你自己的冒险游戏。我正在使用闲置平台。这是我的代码示例。

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   

显然,action1playAgain之间存在内容。我想替换again = false

中的elif playAgain =="N"
elif playAgain =="N":
    again = false

我想跳转到playAgain所以我不只是结束程序。这是我的第一个问题,所以我的格式可能有点奇怪,所以请更正。

1 个答案:

答案 0 :(得分:0)

如上所述,python不支持GoTo。实现相同结果的一种方法是创建一个如下所示的退出游戏功能,然后在主游戏代码的末尾调用它。

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

原始代码:

...
again = quit_game()