Final Capstone: Build a Quiz Engine · practice
Ask One Complete Question
You now have three independent pieces:
read_nonempty_answer(prompt)collects usable text;response_result(question, given)checks that text;a question
supplies the prompt and expected answer.
Compose them into ask_question(question).
Define the caller’s view
The
display the question through
read_nonempty_answer;pass the returned answer to
response_result;print the result’s message;
return the result’s
.
The scoring
was_correct = ask_question(question)
if was_correct:
score += 1
The caller needs only a Boolean. It does not need to know how many invalid attempts occurred or how the message was built.
Let each helper keep its responsibility
Do not copy the retry loop into ask_question. Call read_nonempty_answer.
Do not duplicate answer cleaning or message construction. Call response_result.
Composition should make the new function short because the detailed work already has names.
Use the record’s prompt
Add one trailing space when sending the prompt:
Capital of France?
This leaves the cursor visually separated from the question.
Test correct and incorrect paths
One call should accept an empty attempt before a correct answer. Another should accept a non-empty but incorrect answer immediately.
These are different situations:
empty input is invalid and causes a retry;
a wrong answer is valid input and receives feedback.
The quiz must not keep asking until the answer is correct. That would change the scoring rule and prevent an incorrect response from counting as an attempt.
What is the difference between an empty answer and an incorrect non-empty answer?
Task
Implement ask_question(question).
Call
read_nonempty_answerwith the question’s prompt followed by one space.Call
response_result(question, given)with the returned answer.Print the returned message.
Return the returned
"correct".
Keep every earlier
To try this function with Run, temporarily add these two lines below the definitions and existing assertions:
sample_question = {"prompt": "Name a tree?", "answer": "maple"}
print(ask_question(sample_question))
Try a blank answer followed by MAPLE, then run again with oak. The blank retries, while the non-empty wrong answer completes the question and returns False.
Remove both temporary lines (the sample_question print() call) before Submit and before continuing. Keep the noninteractive assertions; they check the helpers without asking for input.