猜字游戏 在这个游戏中,你必须一个字母一个字母的猜出秘密单词。 如果你猜错了一个字母,你将丢掉一条命。 正如游戏名那样,你需要仔细选择字母,因为你的生命数量非常有限。
import random
lives = 3
# 系统随机抽取单词
word = ['pizza', 'fairy', 'teeth', 'shirt', 'otter', 'plane' ]
secret_word = random.choice(word)
print(secret_word)
heart_symbol = u'\u2764'
print('剩余生命次数:', heart_symbol * lives)
guess_word_correctly = False
# 创建了一个列表
clue = list('?????')
def update_clue(guess_letter,secret_word,clue):
# index 通常是一个用于标识列表中某个元素位置的整数
index = 0
while index < len(secret_word):
if guess_letter == secret_word[index]:
clue[index] = guess_letter
index = index + 1
while lives > 0:
print(clue)
guess = input(" 请输入你想要猜的单词:").lower()
print(guess)
if guess == secret_word:
guess_word_correctly = True
break
elif guess in secret_word:
update_clue(guess,secret_word,clue)
else:
print('错误。你丢了一条命\n')
lives = lives - 1
print('剩余生命次数:' ,heart_symbol* lives)
if guess_word_correctly :
print('你赢了! 秘密单词是 ' + secret_word)
else:
print('你输了! 秘密单词是 ' + secret_word)
pizza
剩余生命次数: ❤❤❤
['?', '?', '?', '?', '?']
请输入你想要猜的单词:d
d
错误。你丢了一条命
剩余生命次数: ❤❤
['?', '?', '?', '?', '?']
请输入你想要猜的单词:p
p
['p', '?', '?', '?', '?']
请输入你想要猜的单词:z
z
['p', '?', 'z', 'z', '?']
请输入你想要猜的单词:i
i
['p', 'i', 'z', 'z', '?']
请输入你想要猜的单词:q
q
错误。你丢了一条命
剩余生命次数: ❤
['p', 'i', 'z', 'z', '?']
请输入你想要猜的单词:a
a
['p', 'i', 'z', 'z', 'a']
请输入你想要猜的单词:pizza
pizza
你赢了! 秘密单词是 pizza
Process finished with exit code 0