why isn't the try again option not working in this code?

Issue

import random

stages = ['''
  +---+
  |   |
  O   |
 /|\  |
 / \  |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|\  |
 /    |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|\  |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|   |
      |
      |
=========''', '''
  +---+
  |   |
  O   |
  |   |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
      |
      |
      |
=========
''', '''
  +---+
  |   |
      |
      |
      |
      |
=========
''']

try_again = True
end_of_game = False
word_list = ["ardvark", "baboon", "camel"]

while try_again:
    chosen_word = random.choice(word_list)
    word_length = len(chosen_word)

    display = []
    for _ in range(word_length):
        display += "_"
    lives = 6
    while not end_of_game:
        guess = input("Guess a letter: ").lower()
        for position in range(word_length):
            letter = chosen_word[position]
            if letter == guess:
                display[position] = letter

        if guess not in chosen_word:
            lives = lives - 1
            if lives == 0:
                end_of_game = True
                print("You lose, try again or quit")


        print(f"{' '.join(display)}")

        if "_" not in display:
            end_of_game = True
            print("You win.")
        print(stages[lives])

        if lives == 0:
            x = input("Do you want to try again? (y/n):\n")
            if x == "y":
                try_again = True
            else:
                try_again = False

I created this hangman game while watching a tutorial, it didn’t include any option of trying again once we lose so I tried to create one.
When I press "y" and enter nothing really happens, the output console is just blank.
Can someone please explain what is wrong with this code?
I also tried to put the if statement in the lives == 0 loop but it didn’t work as well.

Solution

You are passing end_of_game to true if the player use all of is trial so when pressing y you will never enter in the second while. The number of lives must be also re-updated to 6 so the player can play again:

import random

stages = ['''
  +---+
  |   |
  O   |
 /|\  |
 / \  |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|\  |
 /    |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|\  |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|   |
      |
      |
=========''', '''
  +---+
  |   |
  O   |
  |   |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
      |
      |
      |
=========
''', '''
  +---+
  |   |
      |
      |
      |
      |
=========
''']

try_again = True
end_of_game = False
word_list = ["ardvark", "baboon", "camel"]

while try_again:
    chosen_word = random.choice(word_list)
    word_length = len(chosen_word)

    display = []
    for _ in range(word_length):
        display += "_"
    lives = 6
    while not end_of_game:
        guess = input("Guess a letter: ").lower()
        for position in range(word_length):
            letter = chosen_word[position]
            if letter == guess:
                display[position] = letter

        if guess not in chosen_word:
            lives = lives - 1
            if lives == 0:
                end_of_game = False
                lives = 6
                print("You lose, try again or quit")


        print(f"{' '.join(display)}")

        if "_" not in display:
            end_of_game = True
            print("You win.")
        print(stages[lives])

        if lives == 0:
            x = input("Do you want to try again? (y/n):\n")
            if x == "y":
                try_again = True

            else:
                try_again = False

Answered By – STM

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published