Before we start, lets create an instance of the Quizit class and load some multiple choice questions.
from dsci524_group13_quizit import Quizit, QuestionType
quiz = Quizit()
mcq_file_path = "../tests/test_data/multiple_choice.csv"
mc_questions = quiz.load_questions(input_file=mcq_file_path, question_type=QuestionType.MULTIPLE_CHOICE, delimiter=";")
Taking a Multiple-Choice Quiz¶
Once you’ve loaded your questions using the load_questions method, you can use the take_multiple_choice method to take a quiz.
The method takes four arguments:
n: Specify how many questions you want in your quiz.
save_questions: Choose whether to save logs of the questions you answered (“correct”, “incorrect”, “all”, or False).
save_score: Save your quiz score by setting this to True.
file_path: Specify where your logs should be saved (optional).
Now, let’s create a multiple-choice quiz with 3 questions.
results = quiz.take_multiple_choice(3)
This quiz contains 3 questions.
To answer, type the corresponding letter of your choice (e.g. A).
If there are multiple answers, separate your choices with commas (e.g. A, B).
==============================
Question 1:
Which of the following are programming languages?
A : Python
B : HTML
C : Java
D : CSS
---------------------------------------------------------------------------
StdinNotImplementedError Traceback (most recent call last)
Cell In[2], line 1
----> 1 results = quiz.take_multiple_choice(3)
File ~/checkouts/readthedocs.org/user_builds/dsci524-group13-quizit/checkouts/latest/src/dsci524_group13_quizit/quizit.py:269, in Quizit.take_multiple_choice(self, n, save_questions, save_score, file_path)
267 while not valid:
268 count += 1
--> 269 user_input = prompt_input()
270 user_input, valid, message = input_check(user_input, n_options, count)
271 print(message)
File ~/checkouts/readthedocs.org/user_builds/dsci524-group13-quizit/checkouts/latest/src/dsci524_group13_quizit/utils.py:20, in prompt_input()
18 def prompt_input():
19 """Prompts the user to input their answer."""
---> 20 return input("Enter Answer: ")
File ~/checkouts/readthedocs.org/user_builds/dsci524-group13-quizit/envs/latest/lib/python3.11/site-packages/ipykernel/kernelbase.py:1281, in Kernel.raw_input(self, prompt)
1279 if not self._allow_stdin:
1280 msg = "raw_input was called, but this frontend does not support input requests."
-> 1281 raise StdinNotImplementedError(msg)
1282 return self._input_request(
1283 str(prompt),
1284 self._parent_ident["shell"],
1285 self.get_parent("shell"),
1286 password=False,
1287 )
StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.
The function have randomly selected 3 questions from the loaded multiple choice questions.
How about a quiz with 6 questions?
results = quiz.take_multiple_choice(6)
This quiz contains 6 questions.
To answer, type the corresponding letter of your choice (e.g. A).
If there are multiple answers, separate your choices with commas (e.g. A, B).
==============================
Question 1:
What is the capital of France?
A : Paris
B : London
C : Berlin
D : Madrid
Your Answer:
Invalid input. Please select a valid option from the given choices.
Your Answer: ['A']
==============================
Question 2:
What are the primary colors?
A : Red
B : Blue
C : Green
D : Yellow
Your Answer: ['A']
==============================
Question 3:
What is the largest ocean on Earth?
A : Atlantic Ocean
B : Indian Ocean
C : Arctic Ocean
D : Pacific Ocean
Your Answer: ['A']
==============================
Question 4:
Who wrote 'To Kill a Mockingbird'?
A : Harper Lee
B : Mark Twain
C : Ernest Hemingway
D : F. Scott Fitzgerald
Your Answer: ['A']
==============================
Question 5:
Which of the following are programming languages?
A : Python
B : HTML
C : Java
D : CSS
Your Answer: ['A']
==============================
Question 6:
Which planet is known as the Red Planet?
A : Earth
B : Mars
C : Jupiter
D : Saturn
Your Answer: aa
Invalid input. Please select a valid option from the given choices.
Your Answer: ['A']
==============================
Quiz Results
Score: 4.25/6 (70.83%)
Time used: 7.61 seconds
There is only 5 questions in the quiz!!
If n is larger than the number of available questions, the method will create a quiz with all available questions (shuffled). In this case, you’ll get a 5-question quiz.
Scoring System¶
Here is how take_multiple_choice calculates your question score:
Each question has correct and incorrect options.
You can select multiple options, but make sure you separate each answer with a comma. (e.g. A, B, C)
You earn points for selecting correct answers and for not selecting incorrect ones.
The score is calculated as the sum of correctly chosen answers and correctly avoided wrong answers, divided by the total number of options.
If no answer is selected or the input is invalid, the score is 0.
Example:
For a question with four options (A, B, C, D) and the correct answer is A and B.
If your answer is A, B and C
You will earn 1 point for selecting the correct answer A.
You will earn 1 point for selecting the correct answer B.
You will earn 0 points for selecting the incorrect answer C.
You will earn 1 point for NOT selecting the incorrect answer D.
Your total score will be for this question will be:
{width=350}
Accessing Your Quiz Results¶
After finishing the quiz, the results object stores everything you need:
results.score: Your total score for the quiz.
results.time_used: How much time you took to complete the quiz.
results.question_summary: A summary of all the questions asked.
results.question_type: The type of question you just took (in this case, “mcq”).
You can access the score, time used, a summary of the questions and the type of question (“mcq”) like this:
results.score
70.83
results.time_used
7.61
results.question_summary
| question | options | answers | explanations | response | score | |
|---|---|---|---|---|---|---|
| 0 | What is the capital of France? | [Paris, London, Berlin, Madrid] | [Paris] | Paris is the capital and most populous city of... | [A] | 1.00 |
| 1 | What are the primary colors? | [Red, Blue, Green, Yellow] | [Red, Blue, Yellow] | The primary colors are red, blue, and yellow. ... | [A] | 0.50 |
| 2 | What is the largest ocean on Earth? | [Atlantic Ocean, Indian Ocean, Arctic Ocean, P... | [Pacific Ocean] | The Pacific Ocean is the largest and deepest o... | [A] | 0.50 |
| 3 | Who wrote 'To Kill a Mockingbird'? | [Harper Lee, Mark Twain, Ernest Hemingway, F. ... | [Harper Lee] | Harper Lee is the author of the Pulitzer Prize... | [A] | 1.00 |
| 4 | Which of the following are programming languages? | [Python, HTML, Java, CSS] | [Python, Java] | Python and Java are programming languages, whi... | [A] | 0.75 |
| 5 | Which planet is known as the Red Planet? | [Earth, Mars, Jupiter, Saturn] | [Mars] | Mars is often called the 'Red Planet' because ... | [A] | 0.50 |
results.question_type
'mcq'
Saving Your Quiz Scores¶
What if you want to keep a record of your quiz scores?
You can use the save_score argument!
results = quiz.take_multiple_choice(3, save_score=True)
This quiz contains 3 questions.
To answer, type the corresponding letter of your choice (e.g. A).
If there are multiple answers, separate your choices with commas (e.g. A, B).
==============================
Question 1:
What is the chemical symbol for water?
A : H2O
B : CO2
C : NaCl
D : O2
Your Answer: ['A']
==============================
Question 2:
Which planet is known as the Red Planet?
A : Earth
B : Mars
C : Jupiter
D : Saturn
Your Answer: ['A']
==============================
Question 3:
Who wrote 'To Kill a Mockingbird'?
A : Harper Lee
B : Mark Twain
C : Ernest Hemingway
D : F. Scott Fitzgerald
Your Answer: ['A']
Score Log Saved to "results"
==============================
Quiz Results
Score: 2.5/3 (83.33%)
Time used: 1.24 seconds
This will create a folder called results in your current working directory and save your quiz score to a file named score_mcq.txt.
Accessing Your Quiz Results¶
Which questions did you answer correctly and incorrectly?
Use the save_question argument to save a log of your incorrect and correct questions.
results = quiz.take_multiple_choice(3, save_questions="all", save_score=True)
This quiz contains 3 questions.
To answer, type the corresponding letter of your choice (e.g. A).
If there are multiple answers, separate your choices with commas (e.g. A, B).
==============================
Question 1:
Who wrote 'To Kill a Mockingbird'?
A : Harper Lee
B : Mark Twain
C : Ernest Hemingway
D : F. Scott Fitzgerald
Your Answer: ['A']
==============================
Question 2:
What are the primary colors?
A : Red
B : Blue
C : Green
D : Yellow
Your Answer: ['A']
==============================
Question 3:
What is the capital of France?
A : Paris
B : London
C : Berlin
D : Madrid
Your Answer: ['A']
Score Log Saved to "results"
Question Log Saved to "results"
==============================
Quiz Results
Score: 2.5/3 (83.33%)
Time used: 7.17 seconds
Here’s what you can do with save_questions:
Set it to
"correct"to save only the correctly answered questions (correct_mcq.txt).Set it to
"incorrect"to save only the incorrectly answered ones (incorrect_mcq.txt).Use
"all"to save both correct and incorrect questions in separate files.Leave it as
False(default) to skip saving questions.
By default, these files will be saved in the results folder.
Saving Logs to a Custom Location¶
If you want your logs saved somewhere else, you can using the file_path argument to specify where you want your records to go.
For example, if you want your files to save to the "quiz_result" directory in your current directory:
results = quiz.take_multiple_choice(3, save_score=True, save_questions="all", file_path="quiz_result")
This quiz contains 3 questions.
To answer, type the corresponding letter of your choice (e.g. A).
If there are multiple answers, separate your choices with commas (e.g. A, B).
==============================
Question 1:
Who wrote 'To Kill a Mockingbird'?
A : Harper Lee
B : Mark Twain
C : Ernest Hemingway
D : F. Scott Fitzgerald
Your Answer: ['A']
==============================
Question 2:
Which of the following are programming languages?
A : Python
B : HTML
C : Java
D : CSS
Your Answer: ['A']
==============================
Question 3:
What is the largest ocean on Earth?
A : Atlantic Ocean
B : Indian Ocean
C : Arctic Ocean
D : Pacific Ocean
Your Answer: ['B']
Score Log Saved to "quiz_result"
Question Log Saved to "quiz_result"
==============================
Quiz Results
Score: 2.25/3 (75.0%)
Time used: 18.15 seconds
Now, your logs are saved to the "quiz_result" directory. If the folder doesn’t exist, it will be created automatically.
Now lets take a look at the files created.
file1 = open('quiz_result/score_mcq.txt')
print(file1.read())
Date | Score |Time Used (s)
Sun Jan 26 10:06:23 2025 | 75.0% | 18.15
file2 = open('quiz_result/incorrect_mcq.txt')
print(file2.read())
Question
Which of the following are programming languages?
A : Python
B : HTML
C : Java
D : CSS
Your Answer: ['A']
Correct Answer: ['Python', 'Java']
Explanations: Python and Java are programming languages, while HTML and CSS are markup and style sheet languages, respectively.
==============================
Question
What is the largest ocean on Earth?
A : Atlantic Ocean
B : Indian Ocean
C : Arctic Ocean
D : Pacific Ocean
Your Answer: ['B']
Correct Answer: ['Pacific Ocean']
Explanations: The Pacific Ocean is the largest and deepest of Earth's oceanic divisions.
==============================
file3 = open('quiz_result/correct_mcq.txt')
print(file3.read())
Question
Who wrote 'To Kill a Mockingbird'?
A : Harper Lee
B : Mark Twain
C : Ernest Hemingway
D : F. Scott Fitzgerald
Your Answer: ['A']
Correct Answer: ['Harper Lee']
Explanations: Harper Lee is the author of the Pulitzer Prize-winning novel 'To Kill a Mockingbird'.
==============================
And there you have it! Your scores and logs are now saved and accessible for later use.