
Checkpoint
Checkpoint: The Keeper's Fuel Log
Every lantern keeper writes down each refill in a fuel log. The Guild wants one tool that adds to a log and adds up the totals. Some keepers scribble notes and smudges in their logs, so be careful when reading.
This checkpoint uses the whole chapter: file modes, reading line
by line, pathlib and catching errors.
Assignment
Each line of a log looks like island,amount, for example
Gull Rock,4.
- Complete
log_fuel(path, island, amount):- Make the folder that will hold
path, with any missing parents. - Add the line
island,amountto the end of the file. Don't erase what is already there!
- Make the folder that will hold
- Complete
fuel_totals(path). Return a dict that maps each island to its total amount (anint).- Skip blank lines and lines starting with
#. - Split each line at the first comma, and strip spaces.
- Skip any line whose amount isn't a whole number.
- If the file doesn't exist, return
{}.
- Skip blank lines and lines starting with
Each line of a log looks like island,amount, for example
Gull Rock,4.
- Complete
log_fuel(path, island, amount):- Make the folder that will hold
path, with any missing parents. - Add the line
island,amountto the end of the file. Don't erase what is already there!
- Make the folder that will hold
- Complete
fuel_totals(path). Return a dict that maps each island to its total amount (anint).- Skip blank lines and lines starting with
#. - Split each line at the first comma, and strip spaces.
- Skip any line whose amount isn't a whole number.
- If the file doesn't exist, return
{}.
- Skip blank lines and lines starting with
main.py Python sleeping
Press Run to try your code, then Check to test it.