如何编写程序自动从文件生成考试题目?

-2 投票
2 回答
4254 浏览
提问于 2025-04-15 15:10

我想写一个程序,能够自动生成一个样本考试。

比如,程序会提示用户提供四个问题类别,这些问题会从以下列表中选出,组成一个6道题的考试:

  1. 循环
  2. 函数
  3. 决策
  4. 数据类型
  5. 内置函数
  6. 递归
  7. 算法
  8. 自顶向下设计
  9. 对象

我还需要提示用户输入考试的总分,以及考试中有多少道选择题。

样本问题、它们的类别、分值(分数)以及是否是选择题的信息都存储在一个名为Questions的文件里,我需要打开这个文件来读取所有的问题。然后,程序应该根据用户输入的内容,从问题文件中随机选择问题。

这个文件的格式是记事本中的文本文件,内容大致如下:

Multiple Choice Questions
Loops Questions
1. Which of the following is not a part of the IPO pattern?
a)Input     b)Program   c)Process   d)Output

2. In Python, getting user input is done with a special expression called.
a)for       b)read      c)simultaneous assignment   d)input

Function Questions
3. A Python function definition begins with
a)def       b)define    c)function  d)defun

4.A function with no return statement returns
a)nothing   b)its parameters    c)its variables     d)None

Decision Questions
5. An expression that evaluates to either true or false is called
a)operational   b)Boolean   c)simple    d)compound

6.The literals for type bool are
a)T,F       b)True,False    c)true,false    d)procrastination

DataTypes Questions
7. Which of the following is not a Python type-conversion function?
a)float     b)round     c)int       d)long

8.The number of distinct values that can be represented using 5 bits is
a)5     b)10        c)32        d)50

Built-in Functions
9.The part of a program that uses a function is called the
a)user      b)caller    c)callee    d)statement

10.A function can send output back to the program with a(n)
a)return    b)print     c)assignment    d)SASE

Recursion
11.Recursions on sequence often use this as a base case:
a)0     b)1     c)an empty sequence d)None

12.The recursive Fibonacci function is inefficient because
a)it does many repeated computations    b)recursion is inherently inefficient compared to iteration
c)calculating Fibonacci numbers is intractable  d)fibbing is morally wrong

Algorithms
13.An algorithm is like a
a)newspaper b)venus flytrap     c)drum      d)recipe

14.Which algorithm requires time directly proportional to the size of the input?
a)linear search b)binary search     c)merge sort    d)selection sort

Top-down design
15.Which of the following is not one of the fundamental characteristics of object-oriented design/programming?
a)inheritance   b)polymorphism      c)generally d)encapsulation

Objects
16.What graphics class would be best for drawing a square?
a)Square    b)Polygon   c)Line      d)Rectangle

17.A user interface organized around visual elements and users actions is called a (n)
a)GUI       b)application   c)windower  d)API

这是我目前写的代码。我该如何改进它呢?

def main():
    infile = open("30075165.txt","r")
    categories = raw_input("Please enter the four categories that are in the exam: ")
    totalmarks = input("Please enter the total marks in the exam: ")
    mc = input("Please enter the amount of multiple choice questions in the exam: ")

main()

2 个回答

2

las3rjock 提出了一个不错的解决方案。

你也可以把你的输入文件放到一个 SQLite 数据库里,使用一种规范化的结构,比如说建立一个问题表和一个答案表(答案表里有一个指向问题的 ID 的外键),然后根据问题的 ID 随机生成一个答案。你还需要一个第三个表来记录每个问题的正确答案。

3

由于缺少回答这个具体问题所需的额外信息,我将简单介绍一下我会用来解决这个问题的一般方法。我的解决方案会使用LaTeX来排版考试内容,并使用probsoln这个工具包来定义题目。

probsoln工具包提供了一种格式,可以用来定义和标记题目,并将它们存储在文件中。它还提供了一个命令\loadrandomproblems[dataset]{n}{filename},可以从filename文件中随机加载n道题目到dataset中。这意味着可以按主题将题目存储在几个外部文件中,比如loops.texfunctions.tex等。然后,你可以写一个Python脚本,根据用户输入,自动生成考试的LaTeX源文件(exam.tex)。

loops.tex

\newproblem{IPOpattern}{Which of the following is not a part of the IPO pattern?
    \\ a) Input \quad b) Program \quad c) Process \quad d) Output}{The correct
    answer goes here.}

\newproblem{input}{In Python, getting user input is done with a special expression
    called: \\ a) for \quad b) read \quad c) simultaneous assignment \quad
    d) input}{The correct answer goes here.}

exam.tex

\documentclass{report}
\usepackage{probsoln}
\begin{document}
\hideanswers
\chapter{Loops}
% randomly select 2 problems from loops.tex and add to
% the data set called 'loops'
\loadrandomproblems[loops]{2}{loops}

% Display the problems
\renewcommand{\theenumi}{\thechapter.\arabic{enumi}}
\begin{enumerate}
\foreachproblem[loops]{\item\label{prob:\thisproblemlabel}\thisproblem}
\end{enumerate}
% You may need to change \theenumi back here

\chapter{Functions}
% randomly select 2 problems from functions.tex and add to
% the data set called 'functions'
\loadrandomproblems[functions]{2}{functions}

% Display the problems
\renewcommand{\theenumi}{\thechapter.\arabic{enumi}}
\begin{enumerate}
\foreachproblem[functions]{\item\label{prob:\thisproblemlabel}\thisproblem}
\end{enumerate}
% You may need to change \theenumi back here

\appendix

\chapter{Solutions}
\showanswers
\begin{itemize}
\foreachdataset{\thisdataset}{%
\foreachproblem[\thisdataset]{\item[\ref{prob:\thisproblemlabel}]\thisproblem}
}
\end{itemize}

\end{document}

撰写回答