如何编写一个程序,从文件中自动生成样本考试题?

时间:2009-10-19 06:39:53

标签: python

如何编写自动生成样本检查的程序?

例如,系统会提示用户提供以下列表中包含在6个问题考试中的四类问题:

  1. 循环
  2. 功能
  3. 决定
  4. 数据类型
  5. 内置功能
  6. 递归
  7. 算法
  8. 自上而下的设计
  9. 物件
  10. 我还需要提示用户提供考试的总分,并提示用户考试中有多少个问题。

    示例问题,类别,价值(商标数量)和 它们是否是多项选择题存储在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 个答案:

答案 0 :(得分:3)

如果没有回答这个问题所需的其他信息,我将概述我将用于解决此问题的一般方法。我的解决方案是使用LaTeX排版考试和probsoln包来定义问题。

probsoln包提供了一种格式,用于定义和标记问题并将其存储在文件中。它还提供命令\loadrandomproblems[dataset]{n}{filename}以将n中随机选择的filename问题加载到dataset。这表明按主题将问题存储在几个外部文件中,例如loops.texfunctions.tex等。然后,您可以编写一个Python脚本,以编程方式根据用户输入为考试(exam.tex)创建LaTeX源。

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}

答案 1 :(得分:2)

las3rjock有一个很好的解决方案。

您还可以使用规范化结构将输入文件移动到SQLite数据库:例如问题表,答案表(使用FK到QuestionID),并根据问题ID生成随机答案。您还需要第三个表来跟踪每个问题的正确答案。