Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Say which error each line gives, before you run it.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. Think about each line alone. The program stops at the first error, so we look at each line as if it were run on its own.
colours[2]: the list has positions 0 and 1 only. An IndexError.ages["Ravi"]: the key is not in the dictionary. A KeyError.int("4.5"): the text has a decimal point, so it is not a whole number. A ValueError."age: " + 12: text plus a number. A TypeError.10 % 0: the remainder of a division by zero. A ZeroDivisionError.total: a name that was never made. A NameError.Check. You can check each one by putting it inside a try and printing type(error).__name__, as in the lesson.
Answer: IndexError, KeyError, ValueError, TypeError, ZeroDivisionError, NameError.
Log in to join the discussion.
Loading…