Skip to content
CodeNest

Applied PythonLesson 19 of 1925 min

Capstone: Build a Quiz App

Put variables, loops, dicts, functions, classes, and error handling together into one working program.

By the end you will be able to

  • Combine the whole course into a single program
  • Structure code as small, testable functions
  • Handle bad input without crashing

Time to build something end to end: a quiz engine that stores questions, scores answers, tracks results, and reports a summary.

Everything it uses has appeared earlier in the course. Read each stage, run it, then tackle the exercises at the bottom.

Stage 1 — the data

Questions are a list of dicts. Keeping data separate from logic means adding a question never requires touching the code that scores one.

Python
Output
1. What does len('Python') return?  [strings]
2. Which operator gives the remainder?  [operators]
3. What type does 3 / 2 produce?  [numbers]
— expected output; press Run to execute it yourself

Stage 2 — scoring one question

One small function, one job. Easy to reason about and easy to test.

Python
Output
True
False
Rejected: choice must be 0..2
— expected output; press Run to execute it yourself

Stage 3 — the full engine as a class

The class holds the questions and the running results together, and reports on them.

Python
Output
Q1: correct
Q2: wrong
Q3: correct
Q4: wrong

Score: 2/4 (50%) — REVIEW
  revise operators (1 missed)
  revise types (1 missed)
— expected output; press Run to execute it yourself

Exercise

Write grade(score, total) that returns "A" for 90%+, "B" for 80%+, "C" for 70%+, "D" for 60%+, and "F" below that. Return "F" if total is 0 rather than crashing.

Python
Output

Press Run to execute this code.

Exercise

Write summarise(results) where results is a list of (topic, correct) tuples. Return a dict mapping each topic to its percentage correct, rounded to the nearest whole number. Example: [("a", True), ("a", False), ("b", True)]{"a": 50, "b": 100}.

Python
Output

Press Run to execute this code.

Where to go next

You now have the whole core of the language. Natural next steps:

  • Practise — rebuild the quiz app from a blank file without looking. Recall beats rereading.
  • Virtual environments and pippython -m venv .venv then pip install to use third-party packages.
  • Pick a directionrequests and FastAPI for web, pandas for data, pytest for testing.
  • Write tests — the tests behind every exercise on this site are ordinary assert statements. That is genuinely how testing starts.

Check your understanding

0/2 answered
  1. 1.Why keep QUESTIONS as data separate from the scoring function?
  2. 2.In the Quiz class, why is score computed from self.results rather than stored in its own attribute?