Lesson Complete!
The Quiz Loop
You finished the second lesson of Upgrade PyQuiz. The quiz now asks three questions per round, tracks a streak, and lets the player go again.
Here is the complete quiz.py:
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),
]
total = 3
while True:
score = 0
streak = 0
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}")
if is_correct:
score = score + 1
streak = streak + 1
print("Correct!")
if streak >= 3:
print(f" Hot streak! ({streak} in a row)")
else:
streak = 0
print(f"Wrong! The correct answer was option {correct}.")
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!")
print()
again = input("Play again? (y/n): ")
if again != "y":
break
print("Thanks for playing!")What you added:
- QUESTIONS list — stores all question data as tuples; adding a question means adding one line
- for loop — iterates over QUESTIONS, unpacking each tuple into named variables
- Score tracking —
scorecounts correct answers,totaltracks how many questions ran - elif chain — grades the final score (perfect, great, not bad, keep practicing)
- Streak tracking — consecutive correct answers increment
streak; a wrong answer resets it - while True + break — runs the quiz in a loop; the player controls when to stop
What's still missing: quiz.py is getting long. All the quiz logic is in one flat block inside the while loop. In the next lesson, you will extract it into named functions — ask_question, show_results, and run_quiz — so each piece has a clear purpose and can be reused independently.