为了账号安全,请及时绑定邮箱和手机立即绑定

Python中的测验游戏循环

Python中的测验游戏循环

翻翻过去那场雪 2021-11-02 15:27:26
class Question:    def __init__(self, prompt, answer):        self.prompt = prompt        self.answer = answer我创建了上面的question_class.py文件并将其导入下面的quiz.py文件。我正在尝试运行一个 while 循环来询问用户是否有兴趣再次播放测验。但是,代码不会运行。如何在循环中再次正确插入播放?另外,我如何询问用户是否准备好玩游戏并正确检查输入错误?这是我的第一个个人项目,并在完成初学者教程后尝试学习如何编写自己的项目。我感谢所有反馈。    from question_class import Question    username = input("What is your name? ")    print(f"Welcome, {username}!")    play_again = 'y'    while play_again == 'y':    question_prompts = [    ]    questions = [        Question(question_prompts[0], "b"),        Question(question_prompts[1], "a"),    ]    def run_test(questions):    score = 0    for question in questions:        answer = input(question.prompt)        if answer == question.answer:            score += 1        print("You answered " + str(score) + "/" + str(len(questions)) + " correct.")    play_again = input("Want to play again(y/n): ")    run_test(questions)
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 148 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号