Apr-20-2020, 03:40 AM
Answer is the same though. When the guess is not in the list index throws an exception.
There is no reason the word can't be a string. The word doesn't change. Here's a "Wheel of Fortune" version of Hangman. It matches multiple positions if a letter occurs in the word more than once.
There is no reason the word can't be a string. The word doesn't change. Here's a "Wheel of Fortune" version of Hangman. It matches multiple positions if a letter occurs in the word more than once.
import random def find_all(letter, word): """Return array of indices where letter appears in word""" return [i for i, x in enumerate(word) if x == letter] def hangman(word): """Play a game of hangman""" # Initialize to empty board word = word.upper() count = len(word) letters = ['_']*count remaining = count misses = 0 # Begin play. Play continues until player guesses the word or runs out of misses while remaining > 0 and misses < 6: """Draw the letters and remaining misses. Get letter guess""" print(' '.join(letters)) letter = input(f'Misses = {misses}. Enter letter: ').upper() """Find all matches""" matches = find_all(letter, word) if len(matches) == 0: misses += 1 # No match else: for i in matches: letters[i] = letter # Replace blanks with letter remaining -= len(matches) # Did they win? if remaining == 0: print('Congratulations!') print(f'The word was "{word}"\n') # Allow for multiple plays words = ['Orangatan', 'Hippopotomus', 'Giraffe', 'Cheetah', 'Chimpanzee'] random.shuffle(words) # Play until out of words or the player wants to quit. first = True for word in words: if not first: if input('Would you like to play again? ').upper()[0] != 'Y': break first = False hangman(word)