Define run_quiz

Extract the round logic into run_quiz and use a default parameter for the question list

💻

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

The while loop body holds everything that makes up one quiz round: resetting score and streak, shuffling questions, looping through each one, and calling show_results. That is a lot of logic spread across the loop.

Extracting it into a function named run_quiz gives the round logic a name — and makes it reusable when you want to run the quiz in different contexts later.

Default parameters

A default parameter provides a fallback value when the caller does not pass one:

def run_quiz(questions=QUESTIONS):
    ...

Calling run_quiz() with no arguments uses QUESTIONS. Calling run_quiz(some_other_list) uses that list instead. You will expand the question bank in the next course — at that point you can pass a filtered subset without touching the function.

len() counts items

Inside run_quiz, use len(questions) to count questions instead of the hardcoded value:

def run_quiz(questions=QUESTIONS):
    total = len(questions)
    score = 0
    streak = 0
    ...

len() returns the number of items in a collection. If the question bank grows from 3 to 30, total updates automatically.

Instructions

Define run_quiz and move the round logic inside it.

  1. After show_results, define a function named run_quiz with one parameter: questions with a default value of QUESTIONS — the default means you can call run_quiz() with no arguments and it uses the full question list automatically.
  2. Inside run_quiz, set total = len(questions), score = 0, and streak = 0len(questions) counts the list dynamically so total stays correct if the list grows, and score and streak reset at the start of every round.
  3. Add print("=== Python Quiz ===") and print() — these print the header the player sees at the start of each round.
  4. After the for loop (still inside run_quiz), call show_results(score, total) — this displays the final score once all questions have been asked.