0%

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

  1. display the question through read_nonempty_answer;

  2. pass the returned answer to response_result;

  3. print the result’s message;

  4. return the result’s .

The scoring will eventually use it like this:

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

  1. Call read_nonempty_answer with the question’s prompt followed by one space.

  2. Call response_result(question, given) with the returned answer.

  3. Print the returned message.

  4. Return the returned "correct" .

Keep every earlier . Submit checks correct and incorrect answers, empty-input retries, and the values passed between your helpers.

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 and the print() call) before Submit and before continuing. Keep the noninteractive assertions; they check the helpers without asking for input.