Refactor ask_question
Exit

Refactor ask_question

Update ask_question to accept a question dict instead of six separate arguments

💻

Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.

QUESTIONS is now a list of dictionaries — the data format has been updated for you. Each dict has four fields:

  • "text" — the question string
  • "options" — a list of four answer strings
  • "correct" — the number of the correct option (1–4)
  • "category" — the topic this question belongs to, such as "loops" or "functions"

The "category" field is new. It lets players filter the quiz to a single topic — for example, practice only "loops" questions before a test. You will wire that up in the next chapter. For now, the field is there in the data; you do not need to use it yet.

The for loop in run_quiz already uses for q in questions_for_round:, so each question arrives as a full dict.

The current ask_question call passes each field individually:

ask_question(q["text"], q["options"][0], q["options"][1], q["options"][2], q["options"][3], q["correct"])

That's six arguments extracted from a dict you already have. There is no need to unpack it — pass q directly and let the function access the fields it needs.

This is a common refactoring pattern: when a function receives many related values that all come from the same object, pass the object instead. The function signature shrinks, the call site shrinks, and adding a new field to the dict later does not require updating the signature.

Instructions

Update ask_question to accept the full question dictionary.

  1. Change the ask_question signature from def ask_question(text, o1, o2, o3, o4, correct): to def ask_question(q): — the function now receives the whole question dict.
  2. Inside ask_question, replace each parameter reference with the corresponding dict lookup:
    • print(text)print(q["text"])
    • print(f" 1) {o1}") through print(f" 4) {o4}")print(f" 1) {q['options'][0]}") through print(f" 4) {q['options'][3]}")
    • is_correct = answer == correctis_correct = answer == q["correct"]
    • print(f"Wrong! The correct answer was option {correct}.")print(f"Wrong! The correct answer was option {q['correct']}.")
  3. In run_quiz, replace the long ask_question(q["text"], q["options"][0], ...) call with ask_question(q).