我该如何让这段代码工作,以便用户可以将他们的猜测输入到函数中,直到他们得到正确猜测的整个单词或者没有更多的生命剩下为止?现在,用户只能输入一个字符串,然后循环突然结束。我无法让我的代码正常循环
secret_words_list = ['voldemort', 'hogwarts']
def hangman():
lives = 5
while lives >= 0:
answer = random.choice(secret_words_list)
guess = raw_input('Write your answer here: ')
hangman_display = ''
for char in answer:
if char in guess:
hangman_display += char
lives -= 1
elif char == ' ':
hangman_display += char
else:
hangman_display += "-"
lives -= 1
if hangman_display == answer:
print("You win")
print(hangman_display)
也许是因为你为每个字符除了''取走了生命? – Julien
也许你可以看到: –
caimaoy
对于每个字符检查你减少了一个生命,而不是包括它在for循环之后(每迭代一次)? – Vinay