Final Capstone: Build a Quiz Engine · practice
Keep Empty Answers Out
The response logic works with fixed
Keep this job in its own
Empty after cleaning is still empty
Both "" (an empty answer) and " " (three spaces) should be rejected. The spaces are characters, but .strip() removes them and returns "".
A text answer such as "0" is not empty and should be accepted. This function validates presence, not correctness.
State the function’s promise
read_nonempty_answer(prompt):
shows the supplied prompt;
collects an answer;
strips surrounding whitespace;
returns the cleaned answer when it is not empty;
otherwise prints
"Please enter an answer."and repeats.
The function does not know the expected quiz answer. It enforces one input rule that every question shares.
Trace the retry path
For a blank answer followed by " maple ":
| Attempt | Cleaned | Action |
|---|---|---|
| first | "" | print guidance and repeat |
| second | "maple" | return "maple" |
break is required.
Keep progress possible
The prompt and input() call must be inside the loop. Each invalid path must reach another input call.
The platform can stop a program that appears not to finish, but the correct design is still a loop with a reachable return path.
Do not compare here
It may be tempting to give this function the question record and check correctness too. Resist that for this milestone.
One function collects valid text. The next function will combine collection with response feedback.
What should read_nonempty_answer() do when the user enters three spaces?
Task
Implement read_nonempty_answer(prompt).
Keep asking with the supplied prompt.
Strip the entered text.
If the cleaned answer is empty, print
Please enter an answer.and repeat.Return the first non-empty cleaned answer.
Keep all completed
To try this function with Run, temporarily add this call below the definitions and existing assertions:
print(read_nonempty_answer("Practice answer: "))
Try an empty answer, a spaces-only answer, and MiXeD . The first two should repeat; the returned text should be MiXeD. Also try 0, which is non-empty text.
Remove the temporary call before Submit and before continuing. Keep the noninteractive assertions; they check the helpers without asking for input.