
Bug hunt
Tidy the Keeper's Code
PEP 8 is Python's style guide. Following it means any Python developer can read your code without stumbling. The naming rules matter most:
| Thing | Style | Example |
|---|---|---|
| variables, functions, methods, modules | snake_case |
total_fuel |
| classes | CapWords |
LanternKeeper |
| constants | UPPER_SNAKE_CASE |
MAX_FUEL |
| internal names | leading _ |
_cache |
Plus: 4-space indents, one statement per line, spaces around = and
operators, and imports at the top. Teams let tools enforce this:
ruff or black format and lint code automatically.
The bug report
A teammate's class works... sort of. When Ada and Bo each add lanterns,
Ada's total includes Bo's fuel! The culprit: Lanterns = [] is
written in the class body, so it's a class attribute shared by every
keeper. Per-object data belongs in __init__, as self.lanterns = [].
Assignment
- Fix the bug: give each keeper its own
lanternslist. - Rename to PEP 8 style:
maxFuelbecomesMAX_FUEL,lantern_keeperbecomesLanternKeeper,Namebecomesname(the parameter and the attribute),Lanternsbecomeslanterns,AddLanternbecomesadd_lantern,TotalFuelbecomestotal_fuel. - Put each statement on its own line.
- Fix the bug: give each keeper its own
lanternslist. - Rename to PEP 8 style:
maxFuelbecomesMAX_FUEL,lantern_keeperbecomesLanternKeeper,Namebecomesname(the parameter and the attribute),Lanternsbecomeslanterns,AddLanternbecomesadd_lantern,TotalFuelbecomestotal_fuel. - Put each statement on its own line.
main.py Python sleeping
Press Run to try your code, then Check to test it.