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

Python 如何编写一个程序,从一个文件中自动生成样本试题?,python,Python,我如何编写一个程序,自动生成样本检查 例如,系统将提示用户从以下列表中提供要包含在a 6题考试中的四类问题: 循环 功能 决定 数据类型 内置函数 递归 算法 自上而下设计 物体 我还需要提示用户提供考试的总分,并提示用户考试中有多少多个问题 样本问题、它们的类别、它们的值(分数)和 它们是否是多项选择题存储在问题文件中 我需要打开阅读所有的问题。然后程序应读取问题文件,并根据用户输入的内容随机选择问题 文件格式为记事本中的文本文件,如下所示: Multiple Choice Questions

我如何编写一个程序,自动生成样本检查

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

  • 循环
  • 功能
  • 决定
  • 数据类型
  • 内置函数
  • 递归
  • 算法
  • 自上而下设计
  • 物体
  • 我还需要提示用户提供考试的总分,并提示用户考试中有多少多个问题

    样本问题、它们的类别、它们的值(分数)和 它们是否是多项选择题存储在
    问题
    文件中 我需要打开阅读所有的问题。然后程序应读取问题文件,并根据用户输入的内容随机选择问题

    文件格式为记事本中的文本文件,如下所示:

    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()
    

    在没有回答这个具体问题所需的额外信息的情况下,我将概述我将用于解决这个问题的一般方法。我的解决方案将涉及使用排版考试和软件包来定义问题

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

    特克斯

    \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.}
    
    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}
    

    las3rjock有一个很好的解决方案


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

    指定了问题文件的格式,或者您需要为它定义一种格式吗?您可以显示输入文件中的几行吗?您一定要使用这种文件格式,还是可以采用不同的格式?