Booyah2G1C
Booyah2G1C

Reputation: 57

How to loop the program back to the beginning with user input

I'm setting up a basic hangman game and am trying to get the program to loop back to the beginning when the game is finished.

    print("Welcome to Hangman")
    print("Start guessing")

    word = "hangman"
    guesses = ''
    turns = 10

    while turns > 0:
        failed = 0
        for char in word:
            if char in guesses:
                print (char),
            else:
                print("_"),
                failed += 1
        if failed == 0:
            print("You won")
            print("Play Again? (y/n)")
            break
        guess = input("Guess a character:")
        guesses += guess
        if guess not in word:
            turns -= 1
            print("wrong")
            print("You have", + turns, "more guesses")
            if turns == 0:
                print("You Lose")
                print("Play again? (y/n)")

Upvotes: 0

Views: 778

Answers (4)

FallenHero
FallenHero

Reputation: 31

You could put it all into a function and have it loop back like this:

def start():
    replay = True
    while (replay):
        game_started()
        inp = input("Play again? Y/n ")
        if inp == 'n' or inp == 'N':
            replay = False

def game_started():
    print("Welcome to Hangman")
    print("Start guessing")

    word = "hangman"
    guesses = ''
    turns = 10

    while turns > 0:
        failed = 0
        for char in word:
            if char in guesses:
                print (char),
            else:
                print("_"),
                failed += 1
        if failed == 0:
            print("You won")
            break
        guess = input("Guess a character:")
        guesses += guess
        if guess not in word:
            turns -= 1
            print("wrong")
            print("You have", + turns, "more guesses")
            if turns == 0:
                print("You Lose")
                break

start()

Edit: Your check if a letter was guesses is also flawed: If you guess "abcdefghijklmnopqrstuvwxyz", you always win. I'd suggest checking the length of the input before appending it to guesses. Also, to print it all into one line, you could use "print(char, end='')" (and "print('_', end='')", respectively). Just make sure to print a newline after the loop, to finish the line.

Upvotes: 0

Vynnova
Vynnova

Reputation: 1

Hey friend here is what I came up with I hope it helps! Instead of using your turns to control the while loop you can use a "running" variable that is set to a boolean which you can use input statements for your replay feature to control when you want to exit (False) or continue through your loop (True).

print("Welcome to Hangman")
print("Start guessing")

word = "hangman"
guesses = ''
turns = 10
running = True

while running:

    failed = 0
    for char in word:
        if char in guesses:
            print(char),
        else:
            print("_"),
            failed += 1

    if failed <= 0:
        print("You won")
        x = input("Play Again? (y/n) \n")
        if x == "y":
            turns = 10
        else:
            running = False

    guess = input("Guess a character: \n")
    guesses += guess

    if guess not in word:
        turns -= 1
        print("wrong")
        print("You have", + turns, "more guesses")

        if turns == 0:
            print("You Lose")
            z = input("Play Again? (y/n) \n")
            if z == "y":
                turns = 10
            else:
                running = False

Upvotes: -1

Nils
Nils

Reputation: 930

You can use an user input and a while loop

play_again = "Y"
while play_again == "Y" or play_again == "y":
    print("Welcome to Hangman")
    print("Start guessing")

    word = "hangman"
    guesses = ''
    turns = 10

    while turns > 0:
        failed = 0
        for char in word:
            if char in guesses:
                print (char),
            else:
                print("_"),
                failed += 1
        if failed == 0:
            print("You won")
            print("Play Again? (y/n)")
            break
        guess = input("Guess a character:")
        guesses += guess
        if guess not in word:
            turns -= 1
            print("wrong")
            print("You have", + turns, "more guesses")
            if turns == 0:
                print("You Lose")
                play_again = input("Play again? (Y/N)")

or in short just put in in a function:

play_again = "Y"
while play_again == "Y" or play_again == "y":
    game()
    play_again = input("Play again? (Y/N)")

Upvotes: 0

Andrew Grass
Andrew Grass

Reputation: 252

Wrap your game in a function and throw it in a while loop. After the play game function, they'll be asked to play again. If they respond with anything but 'y', the loop breaks.

while True:
    # play_game()
    if input("Play again?") != 'y':
        break
print("Thanks for playing!")

Upvotes: 3

Related Questions