开始猜英文单词的游戏…
总计生命次数:3次
-----------游戏开始中…-----------
????请猜一个,4位数的单词:mafr
猜错了,再努力一下
-----------你还有2次生命------------
ma??请猜一个,4位数的单词:math
math恭喜猜中了
import random
print('开始猜英文单词的游戏...')
# 单词列表
words = ['python', 're', 'math', 'tkinter', 'tables', 'encoding']
word = random.choice(words)
guesses = ''
turns = 3
print(f'总计生命次数:{turns}次')
print('游戏开始中...'.center(30, '-'))
while turns > 0:
failed = 0
for char in word:
if char in guesses:
print(char, end="")
else:
print("?", end=""),
failed += 1
if failed == 0:
print("恭喜猜中了")
break
guess = input(f'请猜一个,{len(word)}位数的单词:')
guesses += guess
if guess not in word:
turns -= 1
print('猜错了,再努力一下')
print(f'你还有{turns}次生命'.center(30, '-'))
if turns == 0:
print('生命为0,已经挂了,游戏结束')