随机生成考试问题。基于与问题相关的数字+字母的数字

时间:2014-07-23 16:29:31

标签: python

我要完成大约10项个人考试。每个考试的标题都包含一个月和一年。在每次考试中,我都选择了一些问题,这些问题分配给了我认为与我的工作相关的编号。此外,每个问题编号都有一个分配给它的学习成果,数字值加上一个字母。

每个问题分配了25种可能的学习成果。我目前将数据存储在Excel文档中,如下所示:

enter image description here

以下是主要问题:我想随机生成具有独特学习成果的问题,以便我可以创建25个问题的考试。在生成问题时,我想提出问题编号和来自的考试。

不幸的是,我不太了解Python,想到一种方法可以快速完成我的工作,但我的第一个想法可能是创建一个与每个考试的学习成果相关的问题数字词典,以及然后将每个考试字典放入一个更大的字典中(如果这甚至是这样的话)。但是,我不确定如何在字典上使用随机功能。

2 个答案:

答案 0 :(得分:1)

我认为您通过向词典添加问题而走上了正确的道路。这就是我如何处理这个问题。

首先,填充字典。字典的关键是学习目标,价值观是满足这些学习目标的问题列表。

# Assumption: question_list has tuples (<question_no>,<learning_obj>,<exam_date>)
learning_dict = {}
for question in question_list:
  question_no = question[0]
  learning_obj = question[1]
  if learning_obj in learning_dict:
    learning_dict[learning_obj].append(question_no)
  else:
    learning_dict[learning_obj] = [question_no]

现在你想从每个学习目标中得到一个问题,对吗?您可以使用random.choice,同时迭代字典的项目。

import random

final_question_list = []
for learning_obj, question_list in learning_dict.items():
  random_question = random.choice(question_list)
  final_question_list.append(random_question)

最后,final_question_list将为每个学习目标提供一个随机选择的问题。

答案 1 :(得分:1)

假设您的电子表格包含第四栏,其中包含问题文字:

将电子表格另存为.csv文件,然后

from collections import defaultdict
import csv

outcomes = set()
questions_by_outcome = defaultdict(list)

# load questions from csv
with open("myspreadsheet.csv", "rb") as inf:
    for row in csv.reader(inf):
        q_num, outcome, exam, q_text = row     # unpack row items
        outcomes.add(outcome)
        questions_by_outcome[outcome].append((exam, q_num, q_text))

您的数据现在使用的格式如下:

outcomes = {"2a", "4b", "1d", "2c"}    # set of unique outcome codes

questions_by_outcome = {
    "1d": [
        ("May 2007", "16", "What does abc def?"),
        ("May 2010", "14", "Who did xyz?")
    ],
    "4b": [
        ("May 2007", "4", "What is the airspeed velocity of an unladen swallow?"),
        ("Nov 2004", "6", "Do you by chance have any Gruyere?")
    ]
}

现在每个结果选择一个随机问题:

import random

# randomize the order of outcomes
outcomes = list(outcomes)
random.shuffle(outcomes)

# pick a random question per outcome
my_questions = [(outcome,) + random.choice(questions_by_outcome[outcome]) for outcome in outcomes]

为您留下一个包含每个结果一个问题的列表,例如

my_questions = [
    # outcome, exam, q_num, q_text
    ("4b", "May 2007", "4", "What is the airspeed velocity of an unladen swallow?"),
    ("1d", "May 2010", "14", "Who did xyz?")
]

希望有所帮助。

相关问题