MIT Python公开课Hangman游戏函数实现遇阻求助
Hey there! Let's break down the issues in your code and fix them step by step.
1. Fixing get_guessed_word
First, let's tackle the problems in this function:
- You’re comparing
char == letters_guessed, butletters_guessedis a list of characters (not a single character). This condition will never trigger. Instead, check ifchar is in letters_guessed. - You tried printing
list, which isn’t defined (you meantlistCopy), but the function should return the formatted guessed word string instead of printing it directly—this gives you more flexibility in your game loop. - The list manipulation can be simplified significantly with a comprehension.
Here's the corrected version:
def get_guessed_word(secret_word, letters_guessed): # Build each segment: show the char if guessed, else "_ " guessed_segments = [char + " " if char in letters_guessed else "_ " for char in secret_word] # Join segments into a single string and return it return "".join(guessed_segments)
Quick Explanation:
- The list comprehension iterates over every character in
secret_word, checks if it’s been guessed, and builds the appropriate display segment. - Returning the joined string lets you use it wherever you need (like printing in your main game loop) instead of forcing output inside the function.
2. Fixing get_available_letters (and the state reset issue)
The core problem here is that you’re redefining the full alphabet every time the function runs—so it can’t remember which letters were guessed before. Instead, the function should calculate available letters by filtering out guessed ones from a fixed full alphabet, using the letters_guessed list you track in your main game loop.
Your original code also had these issues:
- Again, comparing
char == letters_guessed(single char vs. list) is incorrect—usechar not in letters_guessedto filter available letters. - You’re modifying and printing a copy of the alphabet, but the function should return the available letters as a usable string.
Here's the corrected version:
def get_available_letters(letters_guessed): full_alphabet = 'abcdefghijklmnopqrstuvwxyz' # Keep only letters that haven't been guessed yet available_letters = [char for char in full_alphabet if char not in letters_guessed] # Join into a clean string to return return "".join(available_letters)
Why the state reset happened:
In your original code, alphabet = ['a','b',...] is created inside the function, so every call starts fresh with the full alphabet. By basing available letters on the letters_guessed list passed in, we let your main game loop track the state of guessed letters—no need to "save" the alphabet inside the function.
Example Game Loop Integration
Here’s how to use these fixed functions with your existing user interaction logic:
secret_word = "hangman" letters_guessed = [] while True: # Display current progress print("\nCurrent word:", get_guessed_word(secret_word, letters_guessed)) print("Available letters:", get_available_letters(letters_guessed)) # Get user input user_guess = input("Guess a letter: ").lower().strip() if user_guess not in letters_guessed: letters_guessed.append(user_guess) else: print("You already guessed that letter! Try again.") # Add win/lose conditions here if set(secret_word).issubset(set(letters_guessed)): print("\nCongratulations! You won!") break # Add logic for wrong guesses and hangman lives here...
This setup keeps track of guessed letters in the main loop, so every call to your functions uses the up-to-date state—no more resetting!
内容的提问来源于stack exchange,提问作者Coderoe

