Python For GCSE
The tasks described on this page should be completed in sequence. The aim of the project is to create an “engine” that can be used to read a data file and deliver a quiz to the user.
Quiz Engine 1: Programming A Multiple Choice Quiz
This is an example of a multiple choice quiz made using Microsoft Forms.
We could say that Microsoft Forms is the quiz "engine". We can use it to design and edit the quiz. We can also use it to "play" the quiz and get scores. The same program can be used to "play" different quizzes.
How about we make a Python "quiz engine". We'll design the quizzes ourselves and put the data into a text file and then write a program that, given a text file in the appropriate format, can play a quiz, asking all of the questions and then give a score and a list of wrong answers at the end.
We'll start quite basic and then work our way up to adding some fancy pants features.
The first thing we need is a file structure for our question data. Let's aim to have all of the details of a question written on a single line of text using specific characters to separate the parts out.
First, list the pieces of information you need about each question in order to be able to ask a question, list multiple choice options for the answer and then check that the user's answer is correct.
Using the list you wrote for Task 1, work out what the first two lines of a text file would be for the question file for the quiz shown in the screenshot.
Quiz Engine 2: Our Text File
Here are the contents of the text file I would use for this,
Bar code Reader|Input,Output,Storage|0
Electronic Ink Display|Input,Output,Storage|2
Magnetic Stripe Reader|Input,Output,Storage|0
Smart Card Reader|Input,Output,Storage|0
Solid State Drive|Input,Output,Storage|2
In words, describe the format being used here. How can you tell what each part of the line means? Describe, in detail, the steps involved in reading each line of this file to get the information needed to be able to ask questions and check answers.
The options in this quiz are the same for every question. Does this format allow you to have different numbers of optional answers for questions in the same quiz?
There are no question numbers or labels for the question options. Assuming we want to show numbers with the questions and lower case letters for the options, do we need to make a change to the format of the file?
Copy the text file contents (only the lines with text on, no blank lines) into a text file and save it as questions.txt.
Quiz Engine 3: Reading The Text File
Let's write some code now to read the text file for us.
We will write a subroutine that accepts the filename as a parameter. The subroutine needs to read the file and the process the text it has read to create,
- A list of the text of the questions
- A list of lists of answer options
- A list of the number of the correct option for each question.
The first part of the process is to read the whole file into a list. We've done this before and can find a reference to this. We will need to add to it though. Read through the following,
Create a folder and place the text file of questions into it.
Answer the following questions about the code shown above,
- What does the statement questions =  do?
- What does it mean to append to a list?
- Describe the data structure that stores the possible answers for a question.
- Why is the int function used?
- The return statement seems to be returning 3 things. What is the correct terminology for what is being done here? (It is not a GCSE level data structure.)
Create a program using the code from above. Save and run it. In the shell, type read_file("questions.txt") to check if you getting the contents of the file and are not getting any errors.
Now type the following in the shell, one line at a time,
q, a, o = read_file("questions.txt") print(q) print(o) print(a)
You should be able to work out what you are seeing. Make any notes that you think you need at this point.
Quiz Engine 4: Playing The Quiz
Now that we can read all of our question data into a program and store it in lists, we need to write out the logic for running a quiz.
We'll do this as a subroutine too. The subroutine will accept the filename of the quiz data as a parameter and then run the quiz. We are looking for this kind of experience during the quiz,
And at the end,
Since we don't have the numbers or the letters in our text file, we need to write code that will generate them for us. Splodged code,
Work out what goes where the splodges are. Hints are,
- A variable storing the name of the file of questions.
- What the score should be before you answer any questions.
- An empty list.
- A variable storing the question number.
- A variable storing the question.
- A number that will help us to convert the code for a lower case letter into a number starting at 0.
- Increase the score by 1.
- The score.
- A variable storing all of the question.
- The list of wrongly answered questions.
Desplodge the code and then test by writing the following line in the shell,
Test several times and fix any errors that you have.
In Notepad (or another text editor), create a second question file to test your program. You don't need too many questions but you should vary the number of possible answers with each question.
Quiz Engine 5: Extending & Improving
Add code at the start of the program allowing the user to enter the name of the quiz that they would like to do. This could be a menu that you set up for question files you have created.
Assume that a valid question file is always used, what else in the code could cause the program to crash? Work out a useful way to deal with it. How can you make it so that, whether or not the user gets the right answer, they never fail to enter something which, at least theoretically, could be an answer.
Question sampling and question shuffling. How about having a really large question file but only ever making a quiz of 10 randomly selected questions. Another cool thing to do would be to shuffle the questions. It is best do this after reading the file but before you separate out the parts of the rows of text. You can use the same method we used for the cards.
This is a little esoteric but can be fun and interesting. You can work out a way of generating questions that sort of make sense, generating a random answer and then testing whether the user gives the answer your code has generated. Make the questions up out of random parts like names, body parts, colours and so on. This can be quite a technically demanding idea but is great fun. A way to get started is to write a sample question and then think of all the linguistically valid ways that it could be varied.
What are the potential difficulties in making a quiz which is not multiple choice? What strategies can you use to ensure that the automated marking works.
Could you knock out some quick code to randomly generate an arithmetic quiz which is automatically marked? Allow the user to make choices about the difficulty level of their quiz, number of questions and so on.
How about adapting the code for your arithmetic quiz so that it generates a file of questions/answers?