我正在尝试编写一个程序,用户必须猜测一个字母以解锁秘密单词。如果在最多 8 次猜测之前正确猜测了秘密单词,则该函数返回 true,否则该函数返回 false。出于某种原因,我的函数无法产生正确的输出。我会输入字母'a',它会打印“迄今为止猜测的字母:['a']”,然后程序将结束。我需要帮助来解决这个问题。
secretWord = 'hello'
lettersGuessed = []
def isWordGuessed(secretWord,lettersGuessed):
guess = 0
while guess <= 8:
secretLetters = list(secretWord)
secretWordLen = len(secretLetters)
letter = input('Enter a letter: ')
lettersGuessed.append(letter)
print('Letters guessed so far: ',lettersGuessed)
if letter not in secretLetters:
guess += 1
while letter in secretLetters:
secretLetters.remove(letter)
if secretLetters == []:
return True
else:
return False
isWordGuessed(secretWord,lettersGuessed)