
Checkpoint
Checkpoint: The Night Watch
Night falls on Gull Rock. The keeper pours fuel into the lantern tank and shouts out each amount. Some shouts are words, some are negative, and some would overflow the tank. Your program must keep the lantern safe all night.
This checkpoint uses everything from this chapter: raise, your
own error type, several except blocks and else.
Assignment
The tank holds at most CAPACITY (10) units.
- Use the recipe to define
TankFullError. - Complete
refill(level, text):- Convert
textwithint(). If it isn't a number, let theValueErrorhappen (don't catch it here). - If the amount is negative, raise
ValueError. - If
level + amountis more thanCAPACITY, raiseTankFullError. - Otherwise, return the new level.
- Convert
- Complete
night_watch(level, shouts). Callrefillfor each shout, in order, and keep the new level when it works. Build a list of notes, one per shout:"ok"if the refill worked,"full"if it raisedTankFullError,"bad"if it raisedValueError.
night_watchreturns a tuple:(final_level, notes).
The tank holds at most CAPACITY (10) units.
- Use the recipe to define
TankFullError. - Complete
refill(level, text):- Convert
textwithint(). If it isn't a number, let theValueErrorhappen (don't catch it here). - If the amount is negative, raise
ValueError. - If
level + amountis more thanCAPACITY, raiseTankFullError. - Otherwise, return the new level.
- Convert
- Complete
night_watch(level, shouts). Callrefillfor each shout, in order, and keep the new level when it works. Build a list of notes, one per shout:"ok"if the refill worked,"full"if it raisedTankFullError,"bad"if it raisedValueError.
night_watchreturns a tuple:(final_level, notes).
main.py Python sleeping
Press Run to try your code, then Check to test it.