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

Summary

In this lesson, you learned that:

  • Python calls errors exceptions. Each type has a name: NameError, TypeError, ValueError, KeyError, IndexError, AttributeError, and ZeroDivisionError for dividing by zero.
  • A traceback has a File line for every call that was running when the exception was raised, from the main program (in <module>) at the top to the line where it happened at the bottom. Read it from the bottom: the type and the message, the line and the function, then who called it. The mistake can be at any of these lines.
  • try and except ValueError: catch an exception. When a line in the try block raises a ValueError, the rest of the block is skipped, the except block runs, and the program goes on.
  • Name the exception you expect. Of several except blocks, the first one whose type matches runs, and except (ValueError, KeyError): catches both types. A bare except: catches everything, your own mistakes too, and hides them.
  • except ValueError as error: stores the exception in a variable, and print(error) prints its message.
  • else runs when the try block raised nothing, and finally always runs.
  • raise ValueError("The age can't be negative.") raises an exception with your message. A function raises one when its data make no sense. Like return, it ends the function.
  • An exception goes up from the function that raised it to the code that called it, until a try catches it. In a loop, a try around one item skips just that item, and the loop goes on.

Now some practice. In the first exercise of this lesson, average() returned 0 for an empty list. But a player without games has no average, and 0 looks like a real one. This time, the function raises an exception.

In the next lesson, Modules, you'll learn to use code that comes with Python: with import, your programs get the mathematical functions of math, random numbers and more.

Instructions

  1. 1

    Write a function average(scores) that returns the average of the points in the list scores. For an empty list, it raises a ValueError with the message No games yet. instead.

  2. 2

    Below, go through players with .items() and print each player's average with one decimal place, such as Anna: 12.0. Call average() in a try block, and when it raises a ValueError, print the message of the exception instead: Tom: No games yet. Keep the points as they are: the exercise also tries other points.

Back

10/10

  1. 1. Reading a Traceback
  2. 2. Catching an Error
  3. 3. Which Error?
  4. 4. Quiz: try and except
  5. 5. The Message of an Error
  6. 6. else and finally
  7. 7. Quiz: else and finally
  8. 8. Raising an Error
  9. 9. Catching Your Own Errors
  10. 10. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.