简体   繁体   中英

Python 3.X jumping to a line of code

I'm trying to make a Choose Your Own Adventure game in 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 . I want to replace again = false in elif playAgain =="N"

elif playAgain =="N":
    again = false

I want to instead jump to playAgain so I don't just end the program. 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 . 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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