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.
- After
show_results, define a function namedrun_quizwith one parameter:questionswith a default value ofQUESTIONS— the default means you can callrun_quiz()with no arguments and it uses the full question list automatically. - Inside
run_quiz, settotal = len(questions),score = 0, andstreak = 0—len(questions)counts the list dynamically sototalstays correct if the list grows, and score and streak reset at the start of every round. - Add
print("=== Python Quiz ===")andprint()— these print the header the player sees at the start of each round. - After the
forloop (still insiderun_quiz), callshow_results(score, total)— this displays the final score once all questions have been asked.
QUESTIONS = [
("What is the type of the value 3.14?", "int", "str", "float", "bool", 3),
("What keyword defines a function in Python?", "for", "def", "if", "class", 2),
("What does print() do?", "Stores a value", "Returns a number", "Displays output", "Reads input", 3),
]
def ask_question(text, o1, o2, o3, o4, correct):
print(text)
print()
print(f" 1) {o1}")
print(f" 2) {o2}")
print(f" 3) {o3}")
print(f" 4) {o4}")
print()
answer_text = input("Your answer (1-4): ")
answer = int(answer_text)
is_correct = answer == correct
print()
print(f"You answered: {answer}")
if is_correct:
print("Correct!")
else:
print(f"Wrong! The correct answer was option {correct}.")
return is_correct
def show_results(score, total):
print(f"Score: {score}/{total}")
if score == total:
print("Perfect score!")
elif score >= 2:
print("Great job!")
elif score >= 1:
print("Not bad!")
else:
print("Keep practicing!")
# Step 1: Define run_quiz(questions=QUESTIONS):
# Step 2: total = len(questions), score = 0, streak = 0
# Step 3: print header and blank line
# Step 5: show_results(score, total)
while True:
score = 0
streak = 0
print("=== Python Quiz ===")
print()
for text, o1, o2, o3, o4, correct in QUESTIONS:
result = ask_question(text, o1, o2, o3, o4, correct)
if result:
score = score + 1
streak = streak + 1
if streak >= 3:
print(f" Hot streak! ({streak} in a row)")
else:
streak = 0
show_results(score, len(QUESTIONS))
print()
again = input("Play again? (y/n): ")
if again != "y":
break
print("Thanks for playing!")
Interactive Code Editor
Sign in to write and run code, track your progress, and unlock all chapters.
Sign In to Start Coding