0%

Final Capstone: Build a Quiz Engine · practice

Keep Empty Answers Out

The response logic works with fixed . Now add the first interactive boundary: collect one non-empty answer.

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):

  1. shows the supplied prompt;

  2. collects an answer;

  3. strips surrounding whitespace;

  4. returns the cleaned answer when it is not empty;

  5. 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 ":

AttemptCleaned Action
first""print guidance and repeat
second"maple"return "maple"

exits both the and the function. No separate 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 from the earlier milestones. Submit checks several non-empty and retry paths.

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.