Minutes per Dog
Time for another project. In this checkpoint, you'll build a report on Mia's week in pieces, as in Build in Small Steps, and give each piece a test before you rely on it, as in Tests Come First. The instructions say less than before about what to write; if you get stuck, look back at the lesson where you learned it.
The report will show how many minutes Mia walked each dog and what she earned for each, her longest walk and her busiest day. In the editor, walks has this week's walks, each a nested list of the day, the dog and the minutes, such as ["Mon", "Rex", 30]. walk_price() works out the price of one walk, £6 for every started half hour. And test_week is a week of three walks that Mia wrote for testing, short enough to work out by hand.
Mia asked for the first piece, the minutes per dog, and pasted the function from the answer she got: minutes_per_dog(). It works, and it counts with a dictionary, totals = {}, which you haven't learned. You could ask for it without one, as in Get Code Explained. But answers count things per name with a dictionary almost every time, the rest of the report needs one too, and the Python course teaches it right after Functions. So this time, learn it from the answer. Before you build on code, though, make sure you can read it.
Instructions
- 1
Attach
script.py, say what you know (lists and nested lists,if, loops and functions) and that dictionaries are new to you, and ask forminutes_per_dog()to be explained line by line, with what you know. Then send it. - 2
Now test it on
test_week. Work out by hand how many minutes Rex and Max walked in it, and undertest_week, write a test for each of them, such asprint(minutes_per_dog(test_week)["Rex"] == …)with your number in place of the dots.["Rex"]reads Rex's minutes from the dictionary that the function returns. Run the program: both tests printTrue. The check also runs them on a brokenminutes_per_dog(), where a good test printsFalse.