使用不同循环中列表中的特定元素进行多选测试python 3.x.

问题描述 投票:-1回答:1

基本上我正在尝试创建一个多项选择测试,它使用存储在列表中的信息来按位置更改问题/答案。

到目前为止,我有这个

import random

DATASETS = [["You first enter the car", "You start the car","You reverse","You turn",
         "Coming to a yellow light","You get cut off","You run over a person","You have to stop short",
         "in a high speed chase","in a stolen car","A light is broken","The car next to you breaks down",
         "You get a text message","You get a call","Your out of gas","Late for work","Driving angry",
         "Someone flips you the bird","Your speedometer stops working","Drinking"],
        ["Put on seat belt","Check your mirrors","Look over your shoulder","Use your turn signal",
         "Slow to a safe stop","Relax and dont get upset","Call 911", "Thank your brakes for working",
         "Pull over and give up","Ask to get out","Get it fixed","Offer help","Ignore it","Ignore it",
         "Get gas... duh","Drive the speed limit","Don't do it","Smile and wave","Get it fixed","Don't do it"],
        [''] * 20,
        ['B','D','A','A','C','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A'],
        [''] * 20]

def main():

    questions(0)
    answers(1)

def questions(pos):
    for words in range(len(DATASETS[0])):
        DATASETS[2][words] = input("\n" + str(words + 1) + ".)What is the proper procedure when %s" %DATASETS[0][words] +
                        '\nA.)'+random.choice(DATASETS[1]) + '\nB.)%s' %DATASETS[1][words] + '\nC.)'
                        +random.choice(DATASETS[1]) + '\nD.)'+random.choice(DATASETS[1])+
                        "\nChoose your answer carefully: ")

def answers(pos):
    for words in range(len(DATASETS[0])):
        DATASETS[4] = list(x is y for x, y in zip(DATASETS[2], DATASETS[3]))
    print(DATASETS)

如果代码对某些人来说很粗糙,我很抱歉......我在上课的第一年,这是我的第一次编程。列表3是我正确答案的关键,我希望我的代码在问题()中改变正确答案的位置,以便它与提供的密钥相关联....

我已经尝试过循环,如果语句和while循环,但只是不能让它做我想象的。任何帮助是极大的赞赏

python-3.x list
1个回答
0
投票
    tmp = "\n" + str(words + 1) + ".)What is the proper procedure when %s" %DATASETS[0][words] + '\nA.)'
    if DATASETS[3][words] == 'A': #if the answer key is A
      tmp = tmp + DATASETS[1][words] #append the first choice as correct choice
    else:
      tmp = tmp + random.choice(DATASETS[1]) #if not, randomise the choice

为'B','C'和'D'做类似的if-else一旦你的问题被提出,那么你可以使用它:

DATASETS[2][words] = input(tmp)

这有点长,但我不确定是否存在任何更短的方式。

© www.soinside.com 2019 - 2024. All rights reserved.