实例1 猜字母/单词游戏
import random
lives=20
words =['pizza','fairy','apple','tree']
secret_word=words[2]
clue = list ('?????')
heart_symbol=u'\u2764'
guessed_word_correctly = False
def update_clue ( guessed_letter , secret_word , clue ):
index = 0
while index < len(secret_word):
if guessed_letter in secret_word[index]:
clue[index] = guessed_letter
index = index + 1
while lives > 0:
print(clue)
print('剩余生命数: ' + heart_symbol * lives)
guess = input('猜测整个单词或者字母: ')
if guess == secret_word:
guessed_word_correctly = True
break
if guess in secret_word:
update_clue(guess , secret_word , clue)
else:
print('有错误,你丢失一条生命\n')
lives = lives - 1
if guessed_word_correctly:
print('you are win ! 秘密单词正是 '+secret_word)
else:
print('you are lose ! 秘密单词是 ' + secret_word)
运行结果: