The Cases Nobody Asked About
A request names the cases you thought of. The program also has to work for the ones you didn't: a week with a single walk, or the week Mia was ill and walked no dogs at all. Programmers call such cases edge cases, and they're where code that works for the example often breaks.
Someone asked for a function average_walk(walks) that returns the average length of a week's walks, and the program in the editor is the answer they got, one of many. Asked Is this function right?, one answer began:
Yes, the function is correct for its purpose. average_walk sums the numbers in the list and divides by the count, which is the definition of an average.Further down, it warned about a week the function can't handle. That helps if you read that far. But “it's correct” is the assistant's opinion, and a list of cases is something you can run. So ask which cases to try, each with what the function should return. The assistant is good at thinking of cases, and the run, not the assistant, says whether the function passes them.
Instructions
- 1
Attach
script.pyand ask which weeks to tryaverage_walk()on before you trust it, each with what the function should return. Then send it. - 2
Try the week with no walks: at the end of the program, add
print("Average:", average_walk([]))and run it. It ends with the error the answer warned you about:Traceback (most recent call last): File "<exec>", line 6, in <module> File "<exec>", line 2, in average_walk ZeroDivisionError: division by zeroFix the function: above its
return, add anifthat returns0when the list is empty (when its length is 0), and run the program again. Keep the lineweek = [30, 45, 60]as it is: the exercise runs your program with other weeks too. It printsAverage: 45.0andAverage: 0.