views:

310

answers:

2

How can I write a program that will automatically generate an sample examination for example The user will be prompted to supply four categories of questions to be included in the a 6 question exam from the following list: Loops,Functions,Decisions,Data Types,Built-in functions, Recursion, Algorithms, Top-down design,Objects.

I also need to prompt the user to supply the total marks of the exam and also prompt the user for how many multiple questions in the exam.

The Sample questions, their category, their value (number of marks) and whether they are multiple choice questions are stored in a Question file that I need to open to read all of the questions. Then the program should read the Question file and randomly select questions according to what the user has entered.

The file format is a text file in notepad.

Here is what is in the text file:Loops Questions

Mutiple 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 speical 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

Decison Questions
5. An expression that evaluates to either true or fales is called
a)opertional    b)Boolean c)simple d)compund

6.The literals for tpe 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-conversiion function?
a)float     b)round  c)int  d)long

8.The number of distinct values that can be represneted 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

Recusrsion
11.Recursions on sequnece 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 ineffcient compared to iteration
c)calcualting Fibonacci numbers is intreactable d)fibbing is morally wrong

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

14.Which alogorithm requires time directly proportional to the size of the input?
a)linear search b)binary search  c)merge sort d)selction 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

This is the code I have gone so far any ideas?



    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  A: 

In the absence of additional information required to answer this specific problem, I will outline the general approach I would use to solve this problem. My solution would involve using LaTeX to typeset the exam and the probsoln package to define problems.

The probsoln package provides a format for defining and labeling problems and storing them in files. It also provides the command \loadrandomproblems[dataset]{n}{filename} to load n randomly-selected problems from filename into dataset. This suggests storing problems by topic in several external files, e.g. loops.tex, functions.tex, etc. Then you could write a Python script to programmatically create the LaTeX source for the exam (exam.tex) based on user input.

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}
las3rjock
+2  A: 

las3rjock has a good solution.

You could also move your input file to a SQLite database, using a normalised structure: e.g. Question table, Answer table (with FK to QuestionID), and generate a random answer based on the Question ID. You'll need a third table to keep track of the correct answer per question too.

Randolph Potter