0

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.

I. Golsby
  • 414
  • 2
  • 4
  • 15
  • 1
    Related [Asking the user for input until they give a valid response](http://stackoverflow.com/q/23294658) – Bhargav Rao Jun 25 '16 at 22:09
  • 1
    IDLE isn't an interpreter, it is a lightweight integrate development environment (IDE) and the `python-idle` tag doesn't seem useful or relevant to the question you are asking.. – juanpa.arrivillaga Jun 25 '16 at 22:14
  • It is unclear to me what you are asking. What exactly is the behavior you are looking for? – juanpa.arrivillaga Jun 25 '16 at 22:15
  • Sorry for being unclear in IDLE. I mentioned IDLE because I'm coding in the location reached from IDLE File>New File and running my code in IDLE. What I want is to jump from the line that says you died to the playAgain part of my code. I understand that in the sample code it does this, but there is stuff between that part and the end in my full code. – I. Golsby Jun 25 '16 at 23:01
  • To my knowledge, Python doesn't have any statements that jump to specific lines of code. Some programming languages do, like Basic, which has the `GOTO` statement. Python doesn't have this functionality, mainly to prevent code spaghetti from happening. Proper use of `if`-`else` statements and loops is the only real way to skip lines and jump backwards in your program. There's also the `break` and `continue` statements, which end a loop and jump back to the beginning of a loop, respectively. `break` and `continue` should be used sparingly, as they can pretty easily spaghettify your code. – Johan Jun 25 '16 at 23:26
  • Thanks for your comment. I also just figured out the answer to what I'm trying to do. What I will now do is put the playAgain stuff into a function (Shoulda thought of that earlier) and just call it when I need it. – I. Golsby Jun 25 '16 at 23:31

1 Answers1

0

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()
Yarnspinner
  • 852
  • 5
  • 7