Choose How Many
Exit

Choose How Many

Ask the player how many questions to answer before each round

💻

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

run_quiz now accepts a num argument, but the while loop still calls run_quiz() with no arguments — always running all ten questions. Asking the player before each round puts them in control.

You already know try/except from the answer-validation loop in ask_question. The same pattern works here: try to convert the input to an integer; if it fails, fall back to all questions.

Pressing Enter without typing anything produces an empty string. Calling int("") raises a ValueError — so you need to check for an empty string before converting, or handle it in the except block.

Instructions

Ask the player how many questions they want before each round.

  1. In the while True: loop, before the run_quiz() call, add a try/except ValueError block — inside try, read input with input(f"How many questions? (1-{len(QUESTIONS)}, enter for all): ") and strip whitespace with .strip(); if the result is an empty string, set num = len(QUESTIONS); otherwise convert it with int() and assign to num — the try/except catches non-numeric input and falls back to all questions.
  2. Replace run_quiz() with run_quiz(num=num) — this passes the player's choice to the function.
  3. Add print() after the try/except block (before run_quiz) — this adds a blank line between the prompt and the quiz header.