Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

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. 1

    Attach script.py and ask which weeks to try average_walk() on before you trust it, each with what the function should return. Then send it.

  2. 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 zero

    Fix the function: above its return, add an if that returns 0 when the list is empty (when its length is 0), and run the program again. Keep the line week = [30, 45, 60] as it is: the exercise runs your program with other weeks too. It prints Average: 45.0 and Average: 0.

Back

3/6

  1. 1. It Runs, and It's Wrong
  2. 2. Quiz: At Least 30 Minutes
  3. 3. The Cases Nobody Asked About
  4. 4. A Result That Looks Right
  5. 5. Quiz: The Limit
  6. 6. Summary
Next

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with: