Ask Three Questions
Exit

Ask Three Questions

Replace the single hardcoded question with a list and loop through all three

💻

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

The quiz has one hardcoded question with five separate variables. To add more questions, you would need five more variables per question — that quickly becomes unmanageable.

The starter code already includes a QUESTIONS list with three questions and a for loop that runs through them. Your job in this chapter is to add the if/else feedback inside that loop.

Read through the explanation below before you start — it covers how QUESTIONS is structured and how the loop works.

The QUESTIONS list

Each question has six pieces of data: the question text, four options, and the number of the correct option. Instead of six separate variables per question, you can group all six values together inside parentheses:

("What is the type of 3.14?", "int", "str", "float", "bool", 3)

This is called a tuple — a fixed group of values in a specific order. The parentheses are what make it a tuple.

Store all your question tuples in a list named QUESTIONS. The list uses square brackets, and each tuple goes on its own line:

QUESTIONS = [
    ("What is the type of 3.14?", "int", "str", "float", "bool", 3),
    ("What keyword starts a loop?", "def", "for", "if", "class", 2),
]

To add a question, you add a line. To remove one, you remove a line. The loop stays unchanged.

For now, the questions are hardcoded directly in the file. In a later course, you will load them from a separate file so the question bank can grow without touching the quiz logic.

Looping over QUESTIONS

When you loop over QUESTIONS, each iteration gives you one tuple — one question's worth of data. You need a way to refer to each value inside it by name.

You do that by listing the names in the for line, in the same order as the values in the tuple:

#              text          o1      o2      o3        o4      correct
#               ↓             ↓       ↓       ↓         ↓        ↓
("What is the type of 3.14?", "int", "str", "float", "bool",    3)

for text, o1, o2, o3, o4, correct in QUESTIONS:
    print(text)
    print(f"  1) {o1}")

On the first iteration, text is "What is the type of 3.14?", o1 is "int", and correct is 3. On the second iteration, all six names take the values from the second tuple.

Instructions

Add if/else feedback inside the loop.

  1. Inside the for loop, after print(f"You answered: {answer}"), write an if statement: if is_correct is True, call print("Correct!").
  2. Add an else clause that calls print(f"Wrong! The correct answer was option {correct}.").