One Step at a Time
The invoice still needs its last four lines: the number of walks, the month's total, the discount and what's left to pay. Ask for all of them at once, and you get a long answer that may rewrite your program, and if something in it is wrong, it could be anywhere. So, as in Build in Small Steps, ask for a plan first, then for one step at a time, and run each step before you ask for the next. When something goes wrong, it's in the step you've just added.
A plan is quick to read and quick to change. If one of its steps isn't what you want, you've lost nothing, since there's no code yet. Plans differ from answer to answer: some work the total out in a function of its own, and some add a running total to print_weeks(). Any of them works, as long as you understand each step.
Instructions
- 1
Attach
script.pyand ask for a plan for the invoice's last four lines,Walks,Total,DiscountandTo pay: the steps in order, without code. Then send it. - 2
Ask in the same chat for the first step only: a function
count_walks(weeks)that returns the number of walks in the month. You don't need to attach the program again, since it hasn't changed. - 3
Paste the function above the line that prints the heading. Under the line that calls
print_weeks(), addcount = count_walks(weeks)and a line that printsWalks:andcount. Run the program: under the weeks, it printsWalks: 10.