…
…
Read questions from a JSON file, print them in a neat format, check the answers and score them; build a report by topic and save the result to a file.
In this project you will write a small quiz engine. The questions are in the questions.json file: each question has its text, its options, its correct option, its topic, and its points. The engine will read the questions, show the options with the letters A, B, C..., check and score the answers, build a report by topic, and write the result to a new JSON file.
Lessons used: functions (14–16), indexes and string methods (20–21), lists, enumerate, and zip (23–24), dictionaries (26–28), comprehensions (30), try, except, and raise (32), JSON files (35).
The player's answers are in a ready-made list. Some answers are in lowercase, some have extra spaces, and one is invalid; the engine must be able to handle them.
What you'll do
Required topics
questions.jsonDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
json.load turns the JSON list in the file into a Python list; each question becomes a dictionary. Open the file with encoding="utf-8" so that accented characters are not garbled.
Print how many questions there are and the total points. The total points is the sum of the points values of all the questions.
This step's code cell is for members.Sign up free
question_text(question, number) must return the text to show on screen: first the numbered question, then each option on its own line with A), B)... The number of options can change from question to question.
In the string LETTERS = "ABCDE", the i-th letter is LETTERS[i]. Collect the lines in a list and join them with "\n".join(...).
This step's code cell is for members.
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
The player can type the answer as "b" or " C ". answer_index(answer, option_count) must clean the answer (strip, upper) and, if it is valid, return the position of the option (A → 0, B → 1...). If it is invalid ("E" on a question with three options, "AB", an empty string), it must raise ValueError.
The valid letters are as many letters as there are options: LETTERS[:option_count].
This step's code cell is for members.Sign up free
The player's answers are ready. Pair each question with its answer (zip), decode the answer, and score it. An invalid answer counts as blank, and the program does not stop. Add the result of each question ("correct", "wrong", "blank") to the results list as a dictionary, to use in the next steps.
This step's code cell is for members.Sign up free
To see which topics are weak, group the results by topic. In the report dictionary, let each topic's value be a [correct count, question count] list. Then print the percentage for each topic.
This step's code cell is for members.Sign up free
Write the result to a file so that the teacher can look at it later: the score, the total, the percentage, and the numbers of the questions that need to be studied again (wrong or blank). Collect the numbers with a list comprehension. After writing the file, read it back and print its contents.
This step's code cell is for members.Sign up free