【问题标题】:Python Beginner - Having trouble with multiple choice quiz programPython 初学者 - 多选测验程序遇到问题
【发布时间】:2020-02-05 07:12:57
【问题描述】:

Python 初学者在这里使用 3.8.1 。

我目前正在开发一个分为两部分的多项选择测验程序。第一部分允许用户创建一系列问题+答案并使用 Pickle 模块保存它们。第二部分允许用户加载他们的保存数据并接受程序的测试。

我的问题是,在程序的第二部分,我想单独显示每个问题的多项选择选项,但我的程序最终会显示每个问题的多项选择,而不仅仅是当前的问题程序要求。

这是什么原因造成的,如何解决!

提前感谢您的帮助!

#this is part one of the program, where the user creates and saves their data
def PartOne():
        import pickle
        import os
        from time import sleep
        os.system('cls')

        while True:
                t = input("How many questions do you have? : ")
                if t.isnumeric():
                        break
                else:
                        print("Please enter a valid number.")

        cx = list()
        ci = list()
        co = list()

        for i in range(0, int(t)):
            c = input("Please enter the question : ")
            ca = input("Please enter option A : ")
            cb = input("Please enter option B : ")
            cc = input("Please enter option C : ")
            cd = input("Please enter option D : ")
            while True:
                m = input("What option coresponds to the correct answer (a, b, c or d) : ")
                if m.lower() in ('a', 'b', 'c', 'd'):
                        cm = {"question": c, "answer": m}
                        cx.append(cm)
                        ch = {"option a": ca, "option b": cb, "option c": cc, "option d": cd}
                        ci.append(ch)
                        cu = {"question": cx, "answer": ci}
                        co.append(cu)
                        break
                else:
                    print("Invalid answer. Please enter «a», «b», «c» or «d»")

        while True:
                question_cm = input("Would you like to save? (yes or no) ")
                if question_cm.lower() in ('yes'):
                        pickle.dump(cx, open("Savedataone.dat", "wb"))
                        pickle.dump(ci, open("Savedatatwo.dat", "wb"))
                        pickle.dump(co, open("Savedatathree.dat", "wb"))
                        print("Saved!")
                        sleep(1.2)
                        break
                if question_cm.lower() in ('no'):
                        print ("Please save to use this information.")
                        sleep(1.2)
                        break
                else:
                        print("Invalid answer. Please enter yes or no.")

#This the second part of the program where the user uses what they inputed in part 1 to quiz themselves

def PartTwo():
        import pickle
        import random
        import os
        from time import sleep
        os.system('cls')

        while True:
                person = input("How much people will participate in this study session? : ")
                if person.isnumeric():
                        break
                else:
                        print("Please enter a valid answer.")

        for i in range(0, int(person)): 

                os.system('cls')
                cx = pickle.load(open("Savedataone.dat", "rb"))
                ci = pickle.load(open("Savedatatwo.dat", "rb"))
                co = pickle.load(open("Savedatathree.dat", "rb"))
                print("Study session will begin soon...")
                sleep(2)

                for cm in cx:
                        random.shuffle(cx)
                        print(cm["question"])
                        print(ci)
                        response = input("What was the correct answer (a, b, c or d)? : ")
                        if (response == cm["answer"]):
                                print("Good!")
                                continue
                        else:
                                print("Wrong...")
                                sleep(0.2)
                                print("The correct answer was", cm, ".")
                                continue
                        print("Study session is now over.")
                        sleep (1)
                print("Next person's study session will begin soon if needed.")
                sleep(1)
                print("Verification...")
                sleep(2.5)

        sleep(1.35)
        print("Study session is now over.") 

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    将您当前的 partTwo() 函数替换为:

    def PartTwo():
            import pickle
            import random
            import os
            from time import sleep
            os.system('cls')
    
            while True:
                    person = input("How much people will participate in this study session? : ")
                    if person.isnumeric():
                            break
                    else:
                            print("Please enter a valid answer.")
    
            for i in range(0, int(person)):
    
                    os.system('cls')
                    cx = pickle.load(open("Savedataone.dat", "rb"))
                    ci = pickle.load(open("Savedatatwo.dat", "rb"))
                    co = pickle.load(open("Savedatathree.dat", "rb"))
                    print("Study session will begin soon...")
                    sleep(2)
                    questionNumber=0
                    for cm in cx:
                            random.shuffle(cx)
                            print(cm["question"])
                            print(ci[questionNumber])
                            response = input("What was the correct answer (a, b, c or d)? : ")
                            if (response == cm["answer"]):
                                    print("Good!")
                                    questionNumber += 1
                                    continue
                            else:
                                    print("Wrong...")
                                    sleep(0.2)
                                    print("The correct answer was", cm, ".")
                                    questionNumber+=1
                                    continue
    
                            print("Study session is now over.")
                            sleep (1)
                    print("Next person's study session will begin soon if needed.")
                    sleep(1)
                    print("Verification...")
                    sleep(2.5)
    
            sleep(1.35)
            print("Study session is now over.")
    

    我注意到您将 PartOne() 中的每个答案都附加到 'ci' 中,但在 partTwo() 中,您只打印 ci 而不指定打印所有内容的部分。

    我添加了一个计数器 (questionNumber) 来跟踪当前提出的问题,该计数器用于指定要给出哪些可能的答案。

    【讨论】:

    • 我收到此错误消息:print(ci["questionNumber"]) TypeError: list indices must be integers or slices, not str
    • 从单词 questionNumber 中删除引号,它使它成为一个字符串,你需要一个整数,因为它指定一个索引。在我的原始代码中你可以看到它没有引号
    • 我不太清楚你添加引号是什么意思。可以举个例子吗?
    猜你喜欢
    • 1970-01-01
    • 2021-05-13
    • 2014-09-28
    • 1970-01-01
    • 2013-12-27
    • 2023-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多