""" 将代码保存为main.py 这是一个使用Python流程控制语句开发的答题闯关游戏,它的一些特点如下: 1.总共有3个关卡,每个关卡有一个题目,每个题目均有3次答题机会。 2.如果从第一关开始,通过所有关卡后视为闯关成功;如果游戏过程中输入q则退出游戏;如果在关卡中用完3次答题机会后则闯关失败。"""# 定义关卡数levels = 3 # 定义问题和答案question1,anwser1 = 'Python 中用于输出内容到控制台的函数是什么?','print'question2,anwser2 = '哪个关键字用于定义函数?','def'question3,anwser3 = 'Python 中列表的 append() 方法的作用是什么?(回答简单描述,如:添加元素)','添加元素'# 定义游戏状态is_playing = True# 进入闯关循环for level in range(1,levels+1): print(f'欢迎来到第{level}关,请听题:\n') # 根据不同关卡,加载不同问题和答案 if level == 1: question,anwser = question1,anwser1 elif level == 2: question,anwser = question2,anwser2 else: question,anwser = question3,anwser3 # 如果本关答题次数大于0,则可以答题,否则结束游戏 anwser_chance = 3 while anwser_chance > 0: user_anwser = input(question) if user_anwser == anwser: print('回答正确!') break elif user_anwser == '': print('您的输入为空,请重新输入答案!') continue elif user_anwser == 'q': print('您已经退出游戏!') is_playing = False else: anwser_chance -= 1 print(f'回答错误!还剩余{anwser_chance}次答题机会!') if anwser_chance == 0: is_playing = False # 每次进入下一关前,检查游戏状态是否为真 if is_playing == False: print('闯关失败,退出游戏~') break# 如果代码执行到这里时,游戏状态为真,那么说明闯关成功if is_playing == True: print('恭喜闯关成功!')