所以我的任务是创建一个单词游戏,它会询问用户一个单词,并在输出中显示带有标记字母的单词,只有字母和大写字母。它显示标记字母(y *) - 如果这个字母在单词中猜猜,如果单词中存在且位置正确,则大写字母;如果单词中没有该字母,则仅使用字母。
问题是,当要猜测的单词是“vigor”并且我正在写“rover”时,在输出中,我得到 r* o* v* e R。但我应该得到 r o* v* e R,因为这个单词中只有一个“r”,而且位置正确。
我尝试更改 letter_finder 函数中 else 的位置并添加更多条件,但只会让情况变得更糟。我觉得答案很简单,但我的思考方向错误,所以这是我的代码:
hidden_word = "vigor"
print(hidden_word)
user_word = input("Guess the word: ")
answer_list = []
def letter_finder():
mistakes = 0
for letter_in_hidden, letter_in_user in zip(hidden_word, user_word):
if hidden_word == user_word:
print("You win!")
break
# print(letter_in_hidden, letter_in_user)
elif letter_in_user in hidden_word and letter_in_user == letter_in_hidden:
answer_list.append(letter_in_user.upper())
" ".join(answer_list)
elif letter_in_user in hidden_word:
answer_list.append(letter_in_user + "*")
" ".join(answer_list)
elif letter_in_user not in hidden_word:
answer_list.append(letter_in_user)
" ".join(answer_list) + "."
# elif letter_in_user == letter_in_hidden:
elif user_word != hidden_word:
mistakes += 1
answer_list.clear()
elif mistakes == 6:
break
print(" ".join(answer_list))
answer_list.clear()
counter = 0
while hidden_word != user_word:
letter_finder()
user_word = str(input("Guess the word: "))
counter += 1
if counter == 5:
print(f"Sorry, you lose. The answer is: {hidden_word}")
break
我可能会首先将这两个单词转换到正确的列表中,以便我们可以对它们进行索引。这还有一个好处是我们不直接修改
hidden_word
。
然后我会处理这些单词两次。一次查找并处理完全匹配,第二次查找并处理部分匹配。
def get_match(user_word, hidden_word):
## ---------------
# covert to proper list and is now a copy
## ---------------
hidden_word = list(hidden_word)
user_word = list(user_word)
## ---------------
## -----------------
## find exact matches and take them out of future matchs
## -----------------
for index, (u, h) in enumerate(zip(user_word, hidden_word)):
if u == h:
user_word[index] = u.upper()
hidden_word[index] = "_"
## -----------------
## -----------------
## find non-exact matches and take them out of future matchs
## -----------------
for index, u in enumerate(user_word):
if u in hidden_word:
user_word[index] += "*"
hidden_word[hidden_word.index(u)] = "_"
## -----------------
return user_word
hidden_word = "vigor"
user_word = "rover"
print(hidden_word, user_word, get_match(user_word, hidden_word))
user_word = "rigor"
print(hidden_word, user_word, get_match(user_word, hidden_word))
user_word = "vigor"
print(hidden_word, user_word, get_match(user_word, hidden_word))
应该返回:
vigor rover ['r', 'o*', 'v*', 'e', 'R']
vigor rigor ['r', 'I', 'G', 'O', 'R']
vigor vigor ['V', 'I', 'G', 'O', 'R']
我相信这就是您正在寻找的:
from collections import defaultdict
def check_answer(user_input, hidden_word, hidden_word_letter_count):
if len(user_input)!=len(hidden_word):
return f"your guess must contain {len(hidden_word)} letters!"
copy = hidden_word_letter_count.copy()
output = ['']*len(hidden_word)
non_matching_indices = []
for i in range(len(hidden_word)):
if user_input[i] == hidden_word[i]:
output[i] = user_input[i].upper()
copy[user_input[i]]-=1
else:
non_matching_indices.append(i)
for i in non_matching_indices:
if user_input[i] != hidden_word[i]:
if copy[user_input[i]]>0:
output[i]=f"{user_input[i]}*"
copy[user_input[i]]-=1
else:
output[i]=user_input[i].lower()
return ''.join(output)
hidden_word = "vigor"
allowed_guesses = 5
hidden_word_letter_count = defaultdict(lambda: 0)
for letter in hidden_word:
hidden_word_letter_count[letter]+=1
wrong_guesses = 0
while wrong_guesses < allowed_guesses:
guess = input("Guess the word: ")
if guess == hidden_word:
print("You win!")
break
else:
print(check_answer(guess,hidden_word,hidden_word_letter_count))
wrong_guesses += 1
if wrong_guesses==allowed_guesses:
print(f"Sorry, you lose. The answer is: {hidden_word}")