It Runs, and It's Wrong
Code from an AI often runs the first time, and code that runs looks finished. It can still be wrong. You find out by running it on the cases that matter to you, not only on the example in your request: the example is the one case the code was written to fit.
Someone asked an AI: Write a Python function walk_price(minutes) that returns the price of a dog walk: 6 for every started half hour. For example, a walk of 45 minutes costs 12. The request says what goes in and what comes out, and it gives an example. The program in the editor is the answer they got. It's one answer of many, since the same request gets a different program each time, and this one runs and gives the right price for 45 minutes.
A rule like “every started half hour” is easiest to get wrong where one half hour ends and the next begins: at the boundary. Is a walk of exactly 60 minutes two half hours or three? No third half hour has started, so it's two, and the walk costs £12. That's the case to try first.
Instructions
- 1
Click Run: the program prints
Price: 12, as the request said. Then try the boundary: below that line, addprint("Price:", walk_price(60))and run the program again. The second line saysPrice: 18, the price of three half hours. - 2
Fix
walk_price(). It always adds a half hour to the whole ones, even when no new one has started. Count the whole half hours with//and add one only whenminutes % 30is more than 0, as the function in Get Code Explained did. Keep the line that prints the price of 45 minutes as it is: the exercise runs your program with other walks too. The program printsPrice: 12twice.