如何在Hangman游戏中实现匹配输入字符后显示对应字母其余字符为短横线
Fixing Your Hangman Game's Letter Display Issue
Hey Ryan, sorry you've been stuck on this for three weeks—let's get this sorted out quickly! The main problem with your current code is that it doesn't keep track of the letters the player has already guessed correctly. Right now, when you guess a right letter, it just prints that single character and then resets everything on the next loop run. Here's how to fix it:
Key Changes Needed:
- Create a display list to track which letters are revealed (use
-for unguessed letters) - Update this display list every time a correct letter is guessed
- Add a check to see if the player has won (all letters revealed)
- Clean up redundant checks and improve user feedback
Modified Full Code:
import random word_list =[ "SAXOPHONE", "THEREMIN", "XYLOPHONE", "TROMBONE", "RECORDER", "OCARINA", "HANG", "GUITAR", "PIANO", "DIDGERIDOO", "CELLO", "CLARINET", "BONGO", "DRUM", "FLUTE", "ERHU", "DIZI", "HARMONICA", "GONG", "GUZHENG" ] pick_word = random.choice(word_list) pick_letter = list(pick_word) # Initialize display with dashes for each letter in the word display = ['-' for _ in pick_word] print("Welcome to Hangman Instrumental") turns = 10 while turns > 0: # Show current game state to the player print("\nCurrent word: ", ''.join(display)) # Get player input (force uppercase) player_in = input("Enter a Character: ").upper() # Validate input is a single alphabet character if len(player_in) != 1 or not player_in.isalpha(): print("Invalid input—please enter a single letter!") continue # Check if letter was already guessed if player_in in display: print("You already guessed that letter! Try another one.") continue # Handle correct guess if player_in in pick_letter: # Update display to show all instances of the correct letter for idx, letter in enumerate(pick_letter): if letter == player_in: display[idx] = player_in print(f"Nice guess! '{player_in}' is in the word.") # Check if player has won if '-' not in display: print(f"\nCongratulations! You won! The word was {pick_word}") break # Handle wrong guess else: turns -= 1 print(f"Oops! '{player_in}' isn't in the word. You have {turns} turns left.") # If loop ends because turns ran out if turns == 0: print(f"\nYou Lose! The word was {pick_word}")
What's Changed:
- Display Tracking: The
displaylist starts with a-for every letter in the secret word. Every time a correct letter is guessed, we replace all matching-in the list with the actual letter. - Game State Visibility: We print the current
displayat the start of each loop so the player can see their progress clearly. - Input Validation: Added checks for non-alphabet characters and already-guessed letters to avoid frustrating the player with repeated mistakes.
- Win Condition: We check if there are no more
-in the display—if so, the player wins and the game ends early. - Redundant Code Removed: Got rid of the duplicate
if player_in in pick_lettercheck to clean up the code.
This should fix your issue where correct guesses only showed a single letter and reset—now the game will keep track of all correct guesses and show the full progress each turn!
内容的提问来源于stack exchange,提问作者Ryan Chandra
相关产品推荐
相关产品推荐

