import random

定义字谜列表

puzzles = ['什么东西走了千万里,还不会说话?', '全身是红肉,头上戴个帽子,走路四条腿,尾巴甩来甩去。', '一只猫,两只猫,三只猫,四只猫,一只猫都没有走。']

定义答案列表

answers = ['鞋子', '蚂蚁', '猫']

定义回答机会次数

chances = 3

while True: # 随机选择一个字谜 index = random.randint(0, len(puzzles)-1) puzzle = puzzles[index] answer = answers[index]

# 初始化回答次数
count = 0

# 进行猜字谜游戏
while count < chances:
    count += 1
    guess = input(puzzle)
    if guess == answer:
        print('猜对了!')
        break
    else:
        print('猜错了,请再接再厉!还剩{}次机会。'.format(chances-count))

# 判断是否继续游戏
play_again = input('是否继续游戏?(输入Y继续,其他键退出)')
if play_again != 'Y':
    break
Python3 猜字谜游戏代码:有趣互动,挑战你的脑力!

原文地址: https://www.cveoy.top/t/topic/oXKf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录