Starts up an interactive game of Hangman. * At the start of the game, let the user know how many letters the secret_word contains and how many guesses s/he starts with. * The user should start with 6 guesses * Before each round, you should display to the user how many gue
(word, initial_tries)
| 141 | |
| 142 | |
| 143 | def play(word, initial_tries): |
| 144 | """ |
| 145 | Starts up an interactive game of Hangman. |
| 146 | |
| 147 | * At the start of the game, let the user know how many |
| 148 | letters the secret_word contains and how many guesses s/he starts with. |
| 149 | |
| 150 | * The user should start with 6 guesses |
| 151 | |
| 152 | * Before each round, you should display to the user how many guesses |
| 153 | s/he has left and the letters that the user has not yet guessed. |
| 154 | |
| 155 | * Ask the user to supply one guess per round. Remember to make |
| 156 | sure that the user puts in a letter! |
| 157 | |
| 158 | * The user should receive feedback immediately after each guess |
| 159 | about whether their guess appears in the computer's word. |
| 160 | |
| 161 | * After each guess, you should display to the user the |
| 162 | partially guessed word so far. |
| 163 | |
| 164 | """ |
| 165 | word_completion = "_" * len(word) |
| 166 | guessed = False |
| 167 | guessed_letters = [] |
| 168 | guessed_words = [] |
| 169 | |
| 170 | tries = initial_tries |
| 171 | |
| 172 | print("\n-------------Welcome to Hangman-------------\n") |
| 173 | print(hangman(tries)) |
| 174 | print(word_completion) |
| 175 | print("\n") |
| 176 | while not guessed and tries > 0: |
| 177 | guess = input("Guess the word:- ").upper() |
| 178 | if len(guess) == 1 and guess.isalpha(): |
| 179 | if guess in guessed_letters: |
| 180 | print("You already guessed the letter", guess) |
| 181 | elif guess not in word: |
| 182 | print(guess, "is not in the word.") |
| 183 | tries -= 1 |
| 184 | guessed_letters.append(guess) |
| 185 | else: |
| 186 | print("Good job,", guess, "is in the word!") |
| 187 | guessed_letters.append(guess) |
| 188 | word_as_list = list(word_completion) |
| 189 | indices = [i for i, letter in enumerate(word) if letter == guess] |
| 190 | for index in indices: |
| 191 | word_as_list[index] = guess |
| 192 | word_completion = "".join(word_as_list) |
| 193 | if "_" not in word_completion: |
| 194 | guessed = True |
| 195 | |
| 196 | elif len(guess) == len(word) and guess.isalpha(): |
| 197 | if guess in guessed_words: |
| 198 | print("You already guessed the word", guess) |
| 199 | elif guess != word: |
| 200 | print(guess, "is not the word.") |