Lesson 06
Multi-Module Python Project — Calculator Case Study
Build a real 3-file Python calculator — calc_menu.py for UI, calcops.py for logic, main.py as controller. The pattern used in professional projects.
Build a real Python project with 3 files. This is the same architecture used in professional projects: one file for UI, one for logic, one for control flow.
Use the playground and quiz at the bottom to run the multi-module calculator demo.
What you will learn
- How to split one application into three focused modules
- Which code belongs in menu, operations, and main
- A complete arithmetic calculator using
from … import - How this 3-file pattern scales to larger apps
- The Module 14 recap — your full import journey
Project architecture
| File | Layer | Job |
|---|---|---|
calc_menu.py |
Presentation | Display the menu only |
calcops.py |
Business logic | Perform arithmetic |
main.py |
Controller | Import, loop, wire everything |
Step 1: Project folder
Imagine you're building a calculator. Instead of writing everything in one file, split it into 3 focused modules.
calculator_project/
├── calc_menu.py # Display menu
├── calcops.py # All math functions
└── main.py # Runs the applicationEach file is its own module. Together they form one application.
Module 1 — Menu (UI layer)
This file has no calculations. Its only responsibility is displaying options.
# calc_menu.py
def show_calc_menu():
print("=" * 50)
print("\tArithmetic Operations")
print("=" * 50)
print("\t\t1.Addition")
print("\t\t2.Subtraction")
print("\t\t3.Multiplication")
print("\t\t4.Division")
print("\t\t5.Modulo Division")
print("\t\t6.Exponentiation")
print("\t\t7.Exit")
print("=" * 50)Real-world analogy: The restaurant menu shows dishes. It does not cook them.
Module 2 — Operations (business logic)
This module performs calculations. It should never ask for input().
# calcops.py
def addop(a, b):
print("sum({},{})={}".format(a, b, a + b))
def subop(a, b):
print("sub({},{})={}".format(a, b, a - b))
def mulop(a, b):
print("mul({},{})={}".format(a, b, a * b))
def divop(a, b):
print("div({},{})={}".format(a, b, a / b))
def modop(a, b):
print("mod({},{})={}".format(a, b, a % b))
def expop(a, b):
print("power({},{})={}".format(a, b, a ** b))Why parameters instead of input()? Functions become reusable in CLI apps, web apps, APIs, and unit tests.
Module 3 — Main controller
This is the brain of the application. It imports other modules and decides what to execute.
# main.py
from calc_menu import show_calc_menu
from calcops import addop, subop, mulop, divop, modop, expop
demo_choices = [(1, 10, 20), (3, 4, 5), (7, None, None)]
for ch, a, b in demo_choices:
show_calc_menu()
print("Choice:", ch)
match ch:
case 1: addop(a, b)
case 2: subop(a, b)
case 3: mulop(a, b)
case 4: divop(a, b)
case 5: modop(a, b)
case 6: expop(a, b)
case 7:
print("Goodbye!")
break
case _:
print("Invalid choice")Simplified demo version:
from calc_menu import show_calc_menu
from calcops import addop, subop
show_calc_menu()
choice = 1
match choice:
case 1:
addop(10, 20)
case 2:
subop(30, 10)How the program runs
The controller coordinates everything — the menu never performs math, and the math module never displays menus.
Who is responsible for what?
| File | Responsibility |
|---|---|
calc_menu.py |
Display the calculator menu |
calcops.py |
Perform arithmetic operations |
main.py |
Import modules, read choices, call functions |
When requirements change:
- New menu item? Edit calc_menu.py only
- New formula? Edit calcops.py only
- Different flow (API instead of CLI)? Edit main.py only
This pattern scales to bigger apps
| App | Menu module | Operations module |
|---|---|---|
| Area calculator | menu.py |
area_ops.py |
| Temperature converter | menu.py |
convert_ops.py |
| Base converter | menu.py |
binary_ops.py |
| Banking app | menu.py |
account_ops.py |
Each project uses menu + ops + main — the same skeleton, different domain logic.
Module 14 path so far
1. Build modules → globals, functions, classes in .py files
2. import → module.feature access (4 syntaxes)
3. from … import → direct name access (3 syntaxes)
4. importlib.reload → pick up edits while running
5. Multi-module app → menu + ops + main (this lesson)
6. Packages → lessons 7–10 — reuse across folders and OS setupInterview cheat sheet
| Concept | Answer |
|---|---|
| One module = | One .py file |
| UI belongs in | calc_menu.py |
| Business logic belongs in | calcops.py |
| Main controller | main.py |
| Best practice | Pass parameters, avoid input() inside modules |
Key takeaway: Professional Python projects separate Presentation (UI), Business Logic, and Controller. This makes code easier to test, maintain, and reuse.
Key takeaways
- One job per file — menu, logic, controller
- from-import is natural for wiring modules in
main.py - Pass parameters — keep
input()out of reusable modules - Same pattern scales to areas, temperature, banking, and more
- Next: Packages Overview — reuse modules across folders, drives, and machines
Loading playground…
Check your understanding
Multi-module project quiz
In the calculator case study, calc_menu.py is responsible for:
1 / 10