Summary
In this lesson, you learned that:
- Code that runs can still be wrong. The example in your request is the one case it was written to fit, so run it on more.
- A rule is easiest to get wrong at a boundary, such as a walk of exactly 60 minutes. Try the boundaries first.
- Ask which cases to try, each with what the function should return, rather than whether the code is right: you get a list to run instead of an opinion. Try the cases nobody asked about, such as an empty week.
- Reading code before you run it is a check too: say what it prints, and when the run disagrees, find out why.
- A result can look right and be wrong. Check a function on numbers small enough to work out by hand: it works the same way on three as on thirty.
Practice it all at once. The program in the editor is an answer someone got when they asked for the price of a week of walks: 6 for every started half hour of each walk. For the week in their request, walks of 45, 20 and 75 minutes, it gives 36, which is right. The assistant works out what each case should give from what you say the function should do, so say that first.
Next comes Finding Mistakes with AI: what to send the assistant when a program ends with an error or prints the wrong thing.
Instructions
- 1
Attach
script.pyand say whatweek_price()should do: 6 for every started half hour of each walk, so a walk of 30 minutes costs 6 and one of 31 minutes costs 12. Ask which weeks to try it on, each with what it should return. Then send it. - 2
Try the boundary: at the end of the program, add
print("Total:", week_price([30, 60]))and run it. Those walks cost 6 and 12, so 18, but the program prints 30. Fixweek_price()and keep the lineweek = [45, 20, 75]as it is: the exercise runs your program with other weeks too. The program printsTotal: 36andTotal: 18.