我在一本似乎與我的 python 版本不一致的書的幫助下編寫了這段代碼。你能幫我解決一下這個問題嗎?它的部分工作。
我收到一些錯誤,例如,guessed_letter 未定義,而我已經在 if 語句中定義了它。請幫忙!新手警報。
隨機導入
生活 = 9
words = ['walk', 'trial', 'books', 'trick', 'brick', 'gloss', 'banks', 'notes',
'投票','率']
secret_word = random.choice(單詞)
提示 = ('_ _ _ _ _ ')
heart_symbol = u'\u2764'
guessed_word_correct = 假
def update_hint(guessed_letter, secret_word, 提示):
索引 = 0
而索引 0:
打印(提示)
print('活著:' + heart_symbol * 活著)
guess = input("猜一個字母或單詞:")
如果猜測 == secret_word:
guessed_word_correct = True
raise Exception('這個詞是正確的')
如果在 secret_word 中猜測:
update_hint(猜測,secret_word,提示)
別的:
print('錯了。少一顆心')
生活 = 生活 - 1
如果guessed_word_correct:
print('你贏了,!!是' + secret_word)
別的:
print('下次更好' + secret_word)
您的代碼中存在很多問題,無論是在縮進方面還是在組織方面。評論描述了其中的許多。這是您的代碼的一個版本,它實際上可以執行您希望它執行的操作。
隨機導入
words = ['walk', 'trial', 'books', 'trick', 'brick', 'gloss', 'banks', 'notes',
'投票','率']
secret_word = random.choice(單詞)
提示 = 列表('_____'[:len(secret_word)])
heart_symbol = u'\u2764'
def update_hint(guessed_letter, secret_word, 提示):
對於範圍內的索引(len(secret_word)):
如果guessed_letter == secret_word[索引]:
提示[索引] = guessed_letter
#print(secret_word)
默認播放():
生活 = 9
而生活> 0:
打印(''.join(提示))
print('活著:' + heart_symbol * 活著)
guess = input("猜一個字母或單詞:")
如果猜測 == secret_word:
guessed_word_correct = True
返回真
如果在 secret_word 中猜測:
update_hint(猜測,secret_word,提示)
別的:
print('錯了。少一顆心')
生活 = 生活 - 1
返回假
如果播放():
print('你贏了,!!它是',secret_word)
別的:
print('下次更好', secret_word)