多项选择题测验,重复你不正确的答案,然后在你全部答对后重新开始

2024-10-02 20:41:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我对编程很陌生(已经学了2个星期了),我的目标是最终设计一个修订风格的选择题测验,随机生成问题并重复你错了的问题。在

我能做到,但我已经走到了死胡同!我希望用户可以选择在正确完成整套问题后再次重复这组问题。在

import random
p2_qns = []
p2_qns.append(["what is 1?", "a", "b", "c", "d", "a"])
p2_qns.append(["what is 2?", "a", "b", "c", "d", "b"])
p2_qns.append(["what is 3?", "a", "b", "c", "d", "c"])

end_2 = False
zero = [0]*len(p2_qns)

def questions():
   end_1 = False
    while end_1 == False:
        p2_qnsc = p2_qns
        #This section picks a random question and asks it
        qn = random.randrange(len(p2_qnsc))
        #this if statement checks to see if the question has alread been          answered correctly.
        if p2_qnsc[qn] != 0:            
            print(p2_qnsc[qn][0],p2_qnsc[qn][1],p2_qnsc[qn][2],p2_qnsc[qn] [3],p2_qnsc[qn][4] )
            #ask for answer and compare it to where the answer is stored
            ans = input("answer:")
            if ans.lower() == p2_qnsc[qn][5]:
                print("correct!")
                #if answer is correct set the whole question to 0 to stop it being asked again
                p2_qnsc[qn] = 0    
            else:
               print("wrong!")
        elif p2_qnsc == zero:
            end_1 = True

while end_2 == False:            
    questions()
    ans = input("Whant to go again? y,n :")
    if ans.lower() == "n":
        end_2 = True
    elif ans.lower() == "y":
        print("starting again")

到目前为止,我的代码将询问我的3个问题,如果回答不正确,会重复这些问题,但是一旦所有问题都得到了回答,它将继续询问如果您选择"y"是否要重新开始。在

任何帮助都将不胜感激-但请记住,我对这一点很陌生。我可以使用ifwhilefor,数组-所以如果它需要更多的工作来工作,我将需要做一些额外的工作!在


Tags: toanswerfalseifisrandomwhatend
1条回答
网友
1楼 · 发布于 2024-10-02 20:41:45

问题是你把问题从列表中完全删除了。相反,将它们标记为已回答。要做到这一点,你需要为每个问题留出一个槽来跟踪这些信息。在构建问题列表时,添加第七个槽并为其赋值False。在

p2_qns.append(["what is 1?", "a", "b", "c", "d", "a", False])
p2_qns.append(["what is 2?", "a", "b", "c", "d", "b", False])
p2_qns.append(["what is 3?", "a", "b", "c", "d", "c", False])

当用户得到正确答案时,请标记:

^{pr2}$

变成

p2_qnsc[qn][6] = True

当测试某个问题是否可以提问时,请选中此字段:

if p2_qnsc[qn] != 0:

变成

if not p2_qnsc[qn][6]:

最后,当用户决定再次回答问题时,请清除上面所做的更改:

for question in p2qnsc:
    question[6] = False

Notes: Assigning p2_qns to p2_qnsc doesn't make a copy of the list. All the changes you make to it are made to p2_qns as well since they both reference the same list. For that reason, you can replace all occurrences of p2_qnsc with p2_qns and drop the p2_qnsc = p2_qns line.

There are many opportunities to improve this code, and there are much better ways of reducing the list of questions to avoid repeatedly choosing the same already-answered question over and over (the randomness in the loop), but that will involve dictionaries. Once you have this code working, you could learn a lot by posting a new question to Code Review.

相关问题 更多 >