Lazo Lab Sign up

App 1: Your First App, a Quiz Game / Build the quiz

Lesson 5 of 10

Store questions in a list

Copying the same if block for every question gets messy. Store each question and answer as a pair in a list, then loop over it. Now adding a question is one line.

Key points

  • A list of (question, answer) tuples
  • for q, a in questions:
  • One loop handles every question
questions = [
    ("What is the capital of Kuwait?", "kuwait city"),
    ("How many bits in a byte?", "8"),
    ("What does CPU stand for?", "central processing unit"),
]

score = 0
for question, correct in questions:
    answer = input(question + " ").strip().lower()
    if answer == correct:
        print("Correct!")
        score += 1
    else:
        print(f"Not quite. The answer was: {correct}")

Your turn

Rewrite your quiz to use a list of at least 5 questions.

Watch a video on thisOpens YouTube search results for “Store questions in a list” in a new tab

Quiz · +10 XP

Why is a list of questions better than copying if blocks?

Log in to save progress and earn XP.