Tests First
Next comes the discount. For a customer's month, Mia has a rule of its own: in a month of 10 walks or more, the customer pays a tenth less, rounded down to whole pounds. Mrs Carter had 10 walks in May for £96. A tenth of that is £9.60, so her discount is £9.
A rule in words leaves things open. Asked for discount(count, total) with only this rule, one answer returned total / 10, which for May is 9.6 instead of 9: it didn't round down. The same message gets a different answer each time, but the others were much like it. So, as in Tests Come First, write the tests before you ask for the code. A test is an example that runs: it calls the function with numbers you've worked out by hand and prints True if the function gets them right. Attached to your message, the tests tell the AI exactly what the function has to do: whether 10 walks count, and how it rounds. Run with its function, they tell you whether it does.
So that the tests can run before the code is there, the editor now has a stand-in discount() above the line that prints the heading, which returns 0 for now. Your tests print False, True and False at first: only the second one passes, since 9 walks really get no discount. Tests fail until the code is written, and when they all print True, you know it was the new code that made them pass.
Instructions
- 1
At the end of the program, write three tests, each on a line of its own:
print(discount(10, 96) == 9),print(discount(9, 96) == 0)andprint(discount(12, 150) == 15). A month of 9 walks gets no discount, and 12 walks for £150 get £15 off. Run the program: the tests printFalse,TrueandFalse. - 2
Attach
script.pyand ask for a functiondiscount(count, total)to replace the one that returns 0, so that your tests pass:countis the number of walks in the month, andtotalis the month's total. Then send it. - 3
Copy the function from the answer and paste it in place of the two lines of
discount()that return 0. Run the program: under the weeks, your tests printTrue,TrueandTrue.