• JUPYTER
  • FAQ
  • View as Code
  • Python 3 (ipykernel) Kernel
  • View on GitHub
  • Execute on Binder
  • Download Notebook
  1. learn-python3
  2. notebooks
  3. beginner
  4. exercises

1. Dealing with exceptions¶

Fill ____ parts of the implementation below. sum_of_list function takes a list as argument and calculates the sum of values in the list. If some element in the list can not be converted to a numeric value, it should be ignored from the sum.

In [ ]:
def sum_of_list(values):
    ____ = 0
    for val in values:
        ____:
            numeric_val = float(val)
        ____ ____ as e:
            ____
        ____ += numeric_val
    return ____
In [ ]:
list1 = [1, 2, 3]
list2 = ["1", 2.5, "3.0"]
list3 = ["", "1"]
list4 = []
list5 = ["John", "Doe", "was", "here"]
nasty_list = [KeyError(), [], dict()]

assert sum_of_list(list1) == 6
assert sum_of_list(list2) == 6.5
assert sum_of_list(list3) == 1
assert sum_of_list(list4) == 0
assert sum_of_list(list5) == 0
assert sum_of_list(nasty_list) == 0

2. Using custom exceptions¶

Implement verify_short_string function which takes a single string as argument. If the length of the input string is more than ten characters, the function should raise TooLongString exception (note: you have to create TooLongString yourself). The function does not have to return anything.

In [ ]:
# Your implementation here
In [ ]:
# These should not raise
verify_short_string("short")
verify_short_string("10   chars")

# This should raise
try:
    verify_short_string("this is long")
except TooLongString as e:
    # This is ok
    pass
else:
    # This means that there was no exception
    assert False

This website does not host notebooks, it only renders notebooks available on other websites.

Delivered by Fastly, Rendered by OVHcloud

nbviewer GitHub repository.

nbviewer version: 8b013f7

nbconvert version: 7.2.3

Rendered (Tue, 10 Jun 2025 07:09:32 UTC)