Organizing a Program Across Files · capstone
Capstone project: Project: Split the Quiz Application into Modules
The chapter began with sixty lines in one file. It ends with a project whose shape you can read.
The finished layout
quizapp/ the application package
__init__.py
models.py Question, Quiz, Attempt
scoring.py percentage, grade_letter, is_pass
report.py interface: facts turned into text
main.py entry point: what running this does
Three layers, and the dependencies only ever point downward. main.py uses the package’s models and report. quizapp.report uses scoring, scoring uses models for is_pass, and models imports nothing above it. A package groups the
The top-level models.py, scoring.py, and report.py from the middle of the chapter are superseded by the package versions. You can delete them from the file panel; the finished program does not import them.
Someone opens main.py and reads only its import lines. What should they learn?
What is being checked
main.pyimports its domain and report fromquizapp, not from the superseded top-level copies.It defines
main(), and calls it only from behind the guard.Importing it prints nothing.
The program prints a headline, the score line, and the grade line, in that order.
Only main.py needs to change. Everything it reaches for is already in the project, written or supplied across the last six lessons.
Finishing
When it runs, read main.py from the top and ask what someone new would learn from each line. Two import lines, the quiz builder and main
Chapter 11 steps back out of the project and into single files again, because the
Task
Wire the finished project together in main.py.
Import Attempt, Question, and Quiz from quizapp.models, and headline and report_lines from quizapp.report. Do not import the superseded top-level models, scoring, or report
Remove the old report_lines definition from main.py, so it cannot overwrite the imported
Keep build_quiz(), which assembles the Chapter 10 Review: “Which keyword starts a function?” answered by def for 2 points, and “Which keyword defines a class?” answered by class for 3 points.
main() builds the quiz, makes an attempt for Mina, answers every question with "def", and prints the headline followed by the report lines. Nothing runs on import, and the file ends with the guard.
Submit when running it prints:
Chapter 10 Review (5 points)
Mina: 2 of 5
Grade: F