Project 5: a quiz engine
Codzy itself has quizzes! Let's code one: the questions are data (a list of dictionaries), kept apart from the engine that runs them and counts the points. Adding a question therefore doesn't require touching the code: that's the principle of data-driven code.
The engine must be forgiving (" B " must count as "b": .strip().lower()), handle questions worth more points, compute a percentage and give useful feedback: the right answer to every missed question. zip walks through questions and answers together.
questions = [
{"q": "2 + 2 * 3 ?", "choices": ["12", "8", "10"], "answer": 1},
{"q": "type(3.0) ?", "choices": ["int", "float", "str"], "answer": 1},
]
for i, item in enumerate(questions, start=1):
print(f"Q{i}. {item['q']}")
for letter, choice in zip("abc", item["choices"]):
print(f" {letter}) {choice}")