Tests First, Then the Code
In Say What the Code Should Do, examples settled what words couldn't. Tests are examples that run, so write them before you ask for the code. Then they do two jobs: they tell the AI exactly what the function has to return, and once you paste in its answer, they check it.
Max is still a puppy, and his walks are short, so Mia is thinking of a lower price for him: £4 for every started quarter hour. A walk of 15 minutes would cost £4, and one of 16 minutes £8, because the second quarter hour has started. The function puppy_price() in the editor is a stand-in: it returns 0 for now, so that the program runs before the real code is there. Your tests print False at first. Tests fail until the code is written, and when they print True, you know it was the new code that made them pass.
This time, send the tests without the rule. The AI then has to work the rule out from them, and if it gets the function right, your tests say what you mean. Choose them so that only Mia's rule fits them. Three tests can fit another rule too: “£4 up to 15 minutes, £8 up to 30 and £12 for any longer walk” passes tests for 15, 16 and 40 minutes, and it isn't Mia's rule. Your tests sit at the end of the first quarter hour and one minute past each quarter hour after it, where her rule adds £4 every time.
Instructions
- 1
Under the function, write four tests:
print(puppy_price(15) == 4), and three more in the same way for walks of 16, 31 and 46 minutes, with the prices you work out. Run the program: the tests printFalse, since the function isn't written yet. - 2
Attach
script.pyand ask for the functionpuppy_price()that makes your tests printTrue, without saying the rule. Then send it. - 3
Copy the function from the answer and paste it in place of the stand-in, the two lines of
puppy_price()that return 0. Run the program: the tests printTruefour times, and the last line isPrice: 20.