Stopping a Python while loop in a text-based adventure game

138 views Asked by At

I am writing a text based adventure game in python language and unfortunately I can't stop the game. I tried writing the code --->

def GAME_RESTART():
    i = input("would you like to play again?(y/n)")
    if i == "y":
        Game()
    elif i == "n":
        print_pause("YOU'VE COME TO AN END!")
        print_pause("Hopefully the game finds you well")
    while i != "y" and i != "n":
        print_pause("Incorrect input. Please choose (y/n)") *#print_pause is a function I defined already in the rest of the code*
        pass
        GAME_RESTART()

Any suggestions how to break the code to stop working?

I actually tried using the command break but it did nothing

1

There are 1 answers

1
Dan Nagle On

Restructure the code to make use of the while loop. There's no need for the function to call itself.

def GAME_RESTART():
    while True:
        i = input("would you like to play again?(y/n)")
        if i == "y":
            Game()
        elif i == "n":
            print_pause("YOU'VE COME TO AN END!")
            print_pause("Hopefully the game finds you well")
            break # exit the loop
        else:
            print("Incorrect input. Please choose (y/n)")