Here is an example of how to create a Python quiz using JSON with only one question and the quiz stops after it:
{
"question": "What is the national animal of Australia?",
"options": [
"Kangaroo",
"Koala",
"Emu",
"Platypus"
],
"answer": "Kangaroo"
}
json
module and read the JSON file:import json
with open("quiz.json") as f:
quiz_data = json.load(f)
print(quiz_data["question"])
for option in quiz_data["options"]:
print(option)
user_answer = input("Your answer: ")
if user_answer == quiz_data["answer"]:
print("Correct!")
else:
print("Incorrect!")
exit()
The complete code looks like this:
import json
with open("quiz.json") as f:
quiz_data = json.load(f)
print(quiz_data["question"])
for option in quiz_data["options"]:
print(option)
user_answer = input("Your answer: ")
if user_answer == quiz_data["answer"]:
print("Correct!")
else:
print("Incorrect!")
exit()
Note: This quiz only has one question, so it will stop after the user answers it. If you want to create a quiz with multiple questions, you will need to modify the code accordingly.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-12-20 11:00:00 +0000
Seen: 7 times
Last updated: Mar 27
How to calibrate two correlated functions simultaneously using Python's scipy.optimize?
Can you explain the functioning of the {m,n}? regex in the Python re module?
Is it possible to open a video file in Python with OpenCV without any delay?
How can one ensure that sub-classes have uniform method parameters in TypeScript?
How can the calculation of matrix determinant be performed using CUDA?
How can code repetition be prevented when using (box)plot functions?