如何实现猜字母游戏中正确字母的位置显示功能?
完善Hangman风格猜字母游戏的猜中字母替换逻辑
需求说明
这是一款类Hangman的猜字母游戏,核心规则:
- 用户可选择生成4位或5位随机单词,初始以对应数量的
*隐藏显示(比如4位单词显示为****) - 用户每次猜测一个字母:
- 猜错时提示错误信息
- 猜对时提示正确,并将隐藏单词中所有匹配位置的
*替换为该字母(例如****变为*a**)
原始代码框架
# Choosing the words if result2 == 4: word = choice(word4) else: word = choice(word5) guess_list = [] # List of guess letters sec_word = [] # the list for formatting the random word for i in range(len(word)): sec_word.append("*") # To format the random word into **** while result1 > 0: # Start of the guessing game on the condition that guess left is greater than 0 print("word is:", "".join(sec_word)) # Displaying the word # Displaying the remaining guess number print("Guess remaining:", num_guess) print("Previous guess:", guess) # Showing the previous guest guess = input("Please guess a letter: ") guess_list.append(guess) # Add the guess letters to a list if guess.lower() == "stop" or guess.lower() == "exit": print("Game Ends!") break # Letting user end the game at anytime if guess not in guess_list: if guess in word: # if the user guess correct letter print(guess, "is in the word") else: # if the user guess wrong letter print("Sorry! letter entered is not present in the word!") else: # If a letter is already in list print("You have already guessed that letter, Try another letter") result1 -= 1 # After each guess the remaining guess time drops 1 time
完善后的代码
from random import choice # 示例单词列表,实际可根据需求扩展 word4 = ["blue", "tree", "book", "cake"] word5 = ["apple", "grape", "house", "chair"] # 模拟用户选择单词长度与初始猜测次数(实际可由用户输入) result2 = 4 result1 = 6 num_guess = result1 guess = "" # 选择目标单词 if result2 == 4: word = choice(word4) else: word = choice(word5) guess_list = [] sec_word = ["*" for _ in range(len(word))] while result1 > 0: print("Word is:", "".join(sec_word)) print("Guesses remaining:", num_guess) print("Previous Guesses:", ", ".join(guess_list) if guess_list else "") guess = input("Choose a letter to guess: ") # 处理退出指令 if guess.lower() in ["stop", "exit"]: print("Game Ends!") break # 处理重复猜测 if guess in guess_list: print(f"‘{guess}’ is NOT in the word and has been guessed before.") continue guess_list.append(guess) # 猜中逻辑:替换对应位置的* if guess in word: print(f"‘{guess}’ is in the word!") for idx, char in enumerate(word): if char == guess: sec_word[idx] = guess # 检查是否完全猜中 if "".join(sec_word) == word: print(f"Congratulations! You guessed the word: {word}") break else: print(f"‘{guess}’ is NOT in the word! Try again!") num_guess -= 1 result1 -= 1
关键修改点
- 猜中字母替换:通过
enumerate遍历目标单词的每个字符与索引,将sec_word对应位置的*替换为猜中字母 - 重复猜测优化:重复猜测不消耗次数,提示语与预期输出一致
- 历史猜测展示:用逗号分隔历史猜测,空状态显示空白
- 胜利判断:当隐藏单词与目标单词完全匹配时,提示胜利并结束游戏
- 次数消耗逻辑:仅猜错时减少剩余猜测次数,符合常规游戏规则
- 变量初始化:避免首次运行时
guess未定义导致的报错
预期输出示例
Word is: **** Guesses remaining: 6 Previous Guesses: Choose a letter to guess: a ‘a’ is NOT in the word! Try again! Word is: **** Guesses remaining: 5 Previous Guesses: a Choose a letter to guess: b ‘b’ is in the word! Word is: b*** Guesses remaining: 5 Previous Guesses: a, b Choose a letter to guess: l ‘l’ is in the word! Word is: bl** Guesses remaining: 5 Previous Guesses: a, b, l Choose a letter to guess: a ‘a’ is NOT in the word and has been guessed before. Word is: bl** Guesses remaining: 5 Previous Guesses: a, b, l Choose a letter to guess: e ‘e’ is in the word! Word is: bl*e Guesses remaining: 5 Previous Guesses: a, b, l, e Choose a letter to guess: u ‘u’ is in the word! Word is: blue Congratulations! You guessed the word: blue
内容的提问来源于stack exchange,提问作者user20006707
相关产品推荐
相关产品推荐

