Python3 猜谜语游戏代码:三次机会,挑战你的脑力!
import random
谜语列表
riddles = [ {'question': '什么东西走路不需要脚?', 'answer': '路'}, {'question': '什么东西越洗越脏?', 'answer': '水'}, {'question': '天黑了,你点了一根蜡烛,第一件事情是什么?', 'answer': '点火'}, {'question': '什么鱼最喜欢喝茶?', 'answer': '泡茶'}, {'question': '什么东西有头无身,有尾无头?', 'answer': '影子'}, {'question': '什么东西装进口袋里越来越轻?', 'answer': '口袋'}, ]
游戏开始
while True: # 选择一条谜语 riddle = random.choice(riddles) print(riddle['question'])
# 设置回答机会
chances = 3
# 循环回答
while chances > 0:
answer = input('请输入答案:')
if answer == riddle['answer']:
print('猜对了!')
break
else:
chances -= 1
if chances > 0:
print('猜错了,还剩%d次机会。' % chances)
else:
print('猜错了,正确答案是%s。' % riddle['answer'])
# 询问是否继续
choice = input('是否继续?(输入“是”或“否”)')
if choice.lower() == '否':
break
print('游戏结束。')
原文地址: https://www.cveoy.top/t/topic/oXSR 著作权归作者所有。请勿转载和采集!