Let’s take some short-answer questions😄¶
▶️Before you start working on the questions, you need to do some preparation!¶
1. Importing Required Libraries and Setting the path¶
You need to import the necessary libraries. pandas is used to handle data (such as loading questions and answers), time is used to track the time, and os and sys are used for path management.
If you’re running your code in Jupyter or another editor, make sure you’ve added the project folder path to the system path, so Python can find the modules you need.
import pandas as pd
import time
import sys
import os
sys.path.append(os.path.join(os.getcwd(), '..'))
from dsci524_group13_quizit.quizit import Quizit
from dsci524_group13_quizit.utils import (score_log, question_log, QuizResult)
2. Loading the Questions you previously update.¶
After the necessary libraries are imported, you need to load the short-answer question data from the CSV file. You should find your short_answer.csv file that contains questions and answers.
❌If the file path is incorrect, Python will throw a FileNotFoundError. Make sure the file path is relative to the script location, or provide an absolute path.
❌If the CSV format is incorrect (missing required columns like question or answers), Python will raise a ValueError. Ensure the CSV has the correct format.
file_path = '../tests/test_data/short_answer.csv'
short_answer_data = pd.read_csv(file_path)
print(short_answer_data.head())
question answers \
0 What continent has the largest population Asia
1 What is the capital of France Paris
2 What is the largest planet in our solar system Jupiter
3 What is the chemical symbol for water H2O
4 Who wrote 'Romeo and Juliet' William Shakespeare
explanations
0 Asia is the most populous continent with over ...
1 Paris is the capital and most populous city of...
2 Jupiter is the largest planet in our solar sys...
3 H2O is the chemical formula for water, consist...
4 William Shakespeare was an English playwright ...
3. Creating the Quizit Instance¶
Now that the question data is successfully loaded, create an instance of the Quizit class and load the question data into it.
If the short_answer_data was not loaded correctly😢, quiz.shrtq will be None, leading to a ValueError when you try to run the quiz.
quiz = Quizit()
quiz.shrtq = short_answer_data
⏸️ Ready to Run the Short Answer Quiz¶
Now you can run the quiz using the take_short_answer() method. This method allows you to specify how many questions to ask, whether to save the questions and scores, and where to save the results.
3 specifies that the quiz will present 3 questions.
save_questions=True means the questions (both correct and incorrect) will be saved to log files.
save_score=True means the final score and the time taken will be saved to a score.txt file.
file_path=”results/” specifies the directory where the logs will be saved.
result = quiz.take_short_answer(1, save_questions=True, save_score=True, file_path="results/")
Question 1: What is the speed of light in vacuum
Hint: Your answer must be two words.
---------------------------------------------------------------------------
StdinNotImplementedError Traceback (most recent call last)
Cell In[4], line 1
----> 1 result = quiz.take_short_answer(1, save_questions=True, save_score=True, file_path="results/")
File ~/checkouts/readthedocs.org/user_builds/dsci524-group13-quizit/checkouts/latest/src/dsci524_group13_quizit/quizit.py:373, in Quizit.take_short_answer(self, n, save_questions, save_score, file_path)
370 elif len(words) == 2:
371 print("Hint: Your answer must be two words.")
--> 373 user_input = input("Enter Answer: ").strip().lower()
375 while len(user_input.split()) not in [1, 2]:
376 print("Invalid answer. The answer must be either one word or two words.")
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.
🎉 Congratulations! You finished the quiz with 100% using only 34.63 seconds yayyy!!¶
Here’s some errors you might face 👇¶
IndexError:Number of Questions Exceeds Available Data¶
If you try to ask more questions than are available in data set, an Error will occur, so make sure that the number of questions you specify (n) does not exceed the number of questions in the loaded dataset👌.
result = quiz.take_short_answer(2, save_questions=True, save_score=True, file_path="results/")
This quiz contains 2 questions.
Question 1: What continent has the largest population
Hint: Your answer must be one word.
Question 2: What is the chemical symbol for water
Hint: Your answer must be one word.
Score Log Saved to "results/"
Question Log Saved to "results/"
==============================
Quiz Results
Score: 1/2 (50.0%)
Time used: 8.91 seconds
TypeError: Invalid Answer Format¶
The take_short_answer() method expects answers to be either one word or two words. If you enter an invalid format,you would get an error “The answer must be either one word or two words”. Please ensure that the user inputs answers in the correct format as the hint shows!🐶
result = quiz.take_short_answer(2, save_questions=True, save_score=True, file_path="results/")
This quiz contains 2 questions.
Question 1: What is the chemical symbol for water
Hint: Your answer must be one word.
Enter Answer: i dont know
Invalid answer. The answer must be either one word or two words.
Enter Answer: H20
Question 2: What continent has the largest population
Hint: Your answer must be one word.
Enter Answer: PARIS
Score Log Saved to "results/"
Question Log Saved to "results/"
==============================
Quiz Results
Score: 0/2 (0.0%)
Time used: 51.44 seconds