Putting it together
- Python course
- Intermediate
- Lesson 46/116
- 6 min
Combine the Quiz class with a menu loop.
Bringing it together
All pieces are ready now: the Question class, the Quiz class, the add_question, play and check_answer methods, and the score attribute. Now we'll combine them into a working mini quiz app.
In the last step you'll create a three-question quiz, answer each one and print the score. We keep the answers hardcoded so the output stays deterministic; in a real app these might come from input(), but here we use fixed values for testability.
A three-question mini quiz
We'll define a quiz with three questions: "Capital?" (answer "Ankara"), "2x2?" (answer "4") and "Summer month?" (answer "June"). We add all three with add_question.
Then we give a guess to each with check_answer. Let the first two be correct and the third wrong. Expected score: 2. This clearly shows the app works.
Practice Run the mini quiz app
All classes above are ready. Now create a quiz; add three questions: Question(text="Capital?", answer="Ankara"), Question(text="2x2?", answer="4") and Question(text="Summer month?", answer="June"). Show the questions with quiz.play(). Then call quiz.check_answer(quiz.questions[0], "Ankara"), quiz.check_answer(quiz.questions[1], "4") and quiz.check_answer(quiz.questions[2], "July"). Finally print the score with print(quiz.score).
Starter code
from dataclasses import dataclass, field
@dataclass
class Question:
text: str = ""
answer: str = ""
@dataclass
class Quiz:
questions: list = field(default_factory=list)
score: int = 0
def add_question(self, q):
self.questions.append(q)
def play(self):
for i, q in enumerate(self.questions, start=1):
print(f"Question {i}: {q.text}")
def check_answer(self, q, guess):
if q.answer == guess:
self.score += 1
return True
return False
# Create a quiz, add three questions, play, check answers, print the scoreSample solution
from dataclasses import dataclass, field
@dataclass
class Question:
text: str = ""
answer: str = ""
@dataclass
class Quiz:
questions: list = field(default_factory=list)
score: int = 0
def add_question(self, q):
self.questions.append(q)
def play(self):
for i, q in enumerate(self.questions, start=1):
print(f"Question {i}: {q.text}")
def check_answer(self, q, guess):
if q.answer == guess:
self.score += 1
return True
return False
quiz = Quiz()
quiz.add_question(Question(text="Capital?", answer="Ankara"))
quiz.add_question(Question(text="2x2?", answer="4"))
quiz.add_question(Question(text="Summer month?", answer="June"))
quiz.play()
quiz.check_answer(quiz.questions[0], "Ankara")
quiz.check_answer(quiz.questions[1], "4")
quiz.check_answer(quiz.questions[2], "July")
print(quiz.score)Expected output
Question 1: Capital? Question 2: 2x2? Question 3: Summer month? 2
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Why keep the answers hardcoded?
- The output is deterministic, testing is easier Correct answer
- The program runs faster
- Classes use less memory
- The input() function fails
Explanation: With hardcoded answers the output is the same every run; testing is easier.
2 Complete the score print line
quiz.check_answer(quiz.questions[2], "Ankara")
print(quiz.____)
quiz.check_answer(quiz.questions[2], "Ankara")
print(quiz.score)
Explanation: The quiz.score attribute gives the score.
3 Match the integration steps
| Term | Meaning |
|---|---|
add_question |
Add a question |
play |
Show questions |
check_answer |
Check the answer |
quiz.score |
Print the score |