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.
- Inside the
forloop, afterprint(f"You answered: {answer}"), write anifstatement: ifis_correctisTrue, callprint("Correct!"). - Add an
elseclause that callsprint(f"Wrong! The correct answer was option {correct}.").
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),
]
print("=== Python Quiz ===")
print()
for text, o1, o2, o3, o4, correct in QUESTIONS:
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}")
# Step 1: If is_correct, print "Correct!"
# Step 2: else print "Wrong! The correct answer was option {correct}."
Interactive Code Editor
Sign in to write and run code, track your progress, and unlock all chapters.
Sign In to Start Coding