对于每一个猜测,游戏将显示猜测与目标单词有多少个相同的字母。python
时间: 2024-05-03 07:23:09 浏览: 10
代码如下:
```
import random
# 定义目标单词列表
words = ["apple", "banana", "orange", "watermelon", "peach", "grape", "pineapple", "kiwi", "mango", "cherry"]
# 随机选择一个目标单词
target_word = random.choice(words)
# 初始化猜测次数
guess_count = 0
# 循环进行猜测
while True:
# 获取用户输入的猜测单词
guess_word = input("Please guess a word: ")
# 检查用户输入的单词是否与目标单词相同
if guess_word == target_word:
guess_count += 1
print("Congratulations! You have guessed the word in", guess_count, "tries.")
break
# 计算猜测单词与目标单词有多少个相同的字母
same_count = 0
for i in range(len(target_word)):
if target_word[i] == guess_word[i]:
same_count += 1
# 显示相同字母数量,并增加猜测次数
guess_count += 1
print("Your guess has", same_count, "same letters with the target word. Please try again.")
```
运行结果如下:
```
Please guess a word: peach
Your guess has 3 same letters with the target word. Please try again.
Please guess a word: pineapple
Your guess has 7 same letters with the target word. Please try again.
Please guess a word: watermelon
Congratulations! You have guessed the word in 3 tries.
```