Test Generator for Teachers
During the long COVID lockdown period when classes were only online, I watched my father, who is a Physics professor, scramble each week to find ways to minimize cheating on exams. Watching him manually alter 30 tests each month by replacing hundreds of numbers with different random numbers made me think of a solution: a Test Generator for Teachers.
I decided to write a program that helped my dad randomize as many tests as he needs and saved him a lot of stress.
The basic principle of the program can be broken up into 6 steps:
1. Import the needed Python libraries, which are NumPy and re.
2. Open the file of the exam in a readable format.
3. Break the text of the file into each question separated by a newline in a form of a list of strings.
4. Find all the numbers in each question.
5. Randomize the numbers with a percentage interval of randomization and replace the original numbers with the randomized ones.
6. Lastly, create the number of tests needed with a different name for each randomized test and with all different numbers and download it to your computer.
Below is a simplified version of my code that would hopefully be helpful to beginners. It works on any computer and accepts .txt files for randomizing. The test has to be formatted in a way so that the number of the question goes first followed by a period, a space, and then the question.
Here is the final code.
Supplies
- Python3 - Must have a Python IDE. Some good examples are Anaconda Navigator, IDLE, or Visual Studio.
- A .txt file for randomization
Import the Python Libraries
If you are just starting to code in python and did not install the Python module NumPy, type pip install NumPy into your terminal.
Then, import all the modules above into your program.
- The re (regular expression) library is used to find substrings of numbers in the text to replace them with randomized numbers.
- The NumPy library is used to randomize each number found in the questions using the margin variable between 0 and 1 that decides by how much the number should increase or decrease.
Open the File From Your Computer
Next, The user is asked to input the name of the file, and the name is stored in a variable called examName. Then, the "exam" variable uses the user's response to open the file in a readable format ("r") and stores the entire text of the exam file in a variable called exam_lines.
Find All the Individual Questions in the File
The function "break_into_Qs" creates a list for all the questions and makes another list for the corresponding line numbers of each question. The counter, c, is used to count the line numbers. The re.findall makes sure to only randomize the numbers found in the text of the question and not the question numbers. Lastly, the actual text of the question and the corresponding number of the question is put into two separate lists.
Randomize the Test
The three functions above (replace_Q_numbers, randomize_exam_Qs, and replace_exam_Qs) all work together to find all the numbers in the text file, randomize the numbers with a percentage interval of randomization, and replaces the original numbers with the randomized numbers in the text.
replace_Q_numbers -
- Determines the number of questions in the file
- Separates each question by putting all the questions in a list
- Finds all the numbers in the text as strings
- Puts all the numbers in a list
- Finds all the numbers as decimals and floats
- Randomizes all the numbers by a percentage interval that is given by the user
- Returns the strings of all the questions with the new, randomized numbers
randomize_exam_Qs -
- Creates a list of the new, randomized questions
- Creates a loop that for every question, the replace_Q_numbers function gets called to randomize the question
- The randomized questions get appended into the list
- The function returns the list
replace_exam_Qs -
- Matches the line numbers with the question
- Returns the lines of all the questions
Make the Files and Save Them to Your Computer
Lastly, it is time to put it all together and create the number of randomized exams needed and download it to your computer.
- Call the break_into_Qs function to retrieve the list with the questions and the line_numbers
- Ask the user to enter a decimal between 0 and 1 that decides by what percentage the number should increase or decrease. For instance, when the user enters the value .2 for the margin, that means that the interval will be between 0.8 and 1.2 percent change of the original number, and it will be uniformly distributed.
- Also, ask the user the number of randomized exams they want to be created
- Create a loop for the number of exams the user entered and call all the necessary functions to randomize the exam
- Finally, download the exams to your computer with the function open() and write()