# # Hangman version 2: h8.py (Fall 2025, CSCI 1470.1) # Two built-in modules may be needed. import sys import random def populate_words(infile_name = 'hangman_words.txt'): """ Read the file infile_name that contains one word per line. Strip away any trailing white spaces. Return a list of words. """ words = [] with open(infile_name, 'r') as file: words= [line.rstrip() for line in file] return words # The number of guesses is set to 13. Captial letters are used for constants. NUM_GUESSES = 13 def get_a_word(words): """ Return a random word from words, which is a list of words. """ return random.choice(words) def get_alphabet_from_user(prompt): """ Prompts the user to enter a single alphabetical character and returns it. Includes validation to ensure only a single letter is entered. """ while True: user_input = input(prompt).strip().lower() if len(user_input) == 1 and user_input.isalpha(): return user_input else: print("Invalid input. Please enter exactly one alphabetical character.") def get_yes_no_answer(prompt): """ Prompt the user to enter a yes no answer. Return True if yes, False if no. """ while True: user_input = input(prompt).strip().lower() if user_input in ('y', 'yes'): return True elif user_input in ('n', 'no'): return False else: print("Invalid input. Please enter 'yes' or 'no'.") def play_one_game(word): """ Play one hangman game. The parameter word is the answer word. """ hidden_word = "-" * len(word) guess = 1 # guessed_chars is a set variable to keep track # of what characters have already been guessed. guessed_chars = set() while guess <= NUM_GUESSES and "-" in hidden_word: print(f"The hidden word: {hidden_word}") while True: user_input = get_alphabet_from_user(f"Enter a character (guess #{guess}): ") if user_input in guessed_chars: print("You have already entered this character. Please input a new one.") else: guessed_chars.add(user_input) break if len(user_input) == 1: # Count the number of times the character occurs in the word num_occurrences = word.count(user_input) # Replace the appropriate position(s) in hidden_word with the actual character. position = -1 for occurrence in range(num_occurrences): position = word.find(user_input, position + 1) # Find the position of the next occurrence hidden_word = (hidden_word[:position] + user_input + hidden_word[position + 1:] ) # Rebuild the hidden word string guess += 1 if not "-" in hidden_word: print("Winner!", end=" ") else: print("Loser!", end=" ") print(f"The word was {word}.") def main(): # populate words dictionary words = [] try: infile_name = 'hangman_words.txt' words = populate_words() except FileNotFoundError: print(f"Error: The file '{infile_name}' was not found.") sys.exit(1) except Exception as e: print(f"An error occurred: {e}") sys.exit(1) # Print a welcome and introduction to the game. print(f""" Welcome to the hangman game to guess a hidden word. The hidden word is initially displayed as a sequence of - of the length of the word. You may make a guess of a character. If the character is in the word, the corresponding - is replaced by the word. You can make up to {NUM_GUESSES} guesses. """) # Play games as many times as the user like. while True: word = get_a_word(words) play_one_game(word) if not get_yes_no_answer('\nPlayer another game [y/n]: '): break; # print print("\nBye, see you later.") if __name__ == "__main__": main()