请求实现无列表版Hangman风格猜单词Python字符串操作程序
Fixing the String-Only Hangman Word Guesser
Got it, let's tackle this string-only word guesser (no lists allowed) and finish that incomplete code. The core idea is to rebuild the display string every time the user guesses a letter—since strings are immutable in Python, we can't modify them in place, so we'll use string concatenation to build new versions each loop.
First, let's fix up the code with clear explanations for each key part:
def word_guess(): # Get the target word from the user, then "clear" the screen word = input('Enter the word: ').lower() # Lowercase to make guesses case-insensitive print('\n' * 100) # Initialize the display string with asterisks matching word length guessed_display = '*' * len(word) # Keep track of letters already guessed to avoid repeats guessed_letters = "" print('Guess the word: ') print(guessed_display) # Loop until all asterisks are replaced (word is fully guessed) while '*' in guessed_display: guess = input('Enter a letter: ').lower().strip() # Validate input: only single letters allowed if len(guess) != 1 or not guess.isalpha(): print("Please enter a single alphabetic character!") continue # Check if letter was already guessed if guess in guessed_letters: print(f"You already guessed '{guess}'! Try another letter.") print(guessed_display) continue # Add the guess to our tracked letters guessed_letters += guess # Rebuild the display string: replace asterisks where the guess matches new_display = "" for idx in range(len(word)): if word[idx] == guess: new_display += guess else: new_display += guessed_display[idx] # Keep existing correct letters/asterisks # Update the display and show it guessed_display = new_display print('\n' * 2) print('Guess the word: ') print(guessed_display) # Once loop ends, user has guessed all letters print("\n🎉 Congratulations! You guessed the word correctly!") # Run the game word_guess()
Key String-Only Logic & Improvements:
- Case Insensitivity: Converted both the target word and user guesses to lowercase so 'A' and 'a' count as the same guess.
- Input Validation: Ensures users only enter single alphabetic characters—no numbers, symbols, or multi-letter inputs.
- Duplicate Guess Handling: Tracks all guessed letters with a string (
guessed_letters) to avoid repeating work and notify users. - String Rebuilding: Instead of lists, we loop through each character index of the target word, build a new
new_displaystring by either keeping the existing correct character/asterisk or replacing it with the correct guess. This strictly uses string concatenation, no list operations. - Clean Display: Added extra newlines to keep the game output readable between guesses.
How It Works Step-by-Step:
- User enters a word, screen clears to hide it.
- A string of asterisks matching the word length is shown.
- User guesses a letter:
- If invalid or already guessed, we prompt again.
- If correct, we rebuild the display string to replace the corresponding asterisks with the guessed letter.
- If wrong, the display string stays the same (but we track the guess to avoid repeats).
- Loop repeats until no asterisks are left—then we celebrate the win!
内容的提问来源于stack exchange,提问作者Chandler Scott
相关产品推荐
相关产品推荐

