Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
For marks = {"Asha": 72, "Ravi": 85}, show the mark of Asha and of Kabir with try and except. What does the program show?
Catch KeyError for a missing key.
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, compare your working with the solution.
Plan. Try marks[name]. If a KeyError happens, say that the name is not in the dictionary.
Asha 72Kabir is not in the dictionary(You could also use marks.get(name). try and except is the general tool, for any kind of error.)
Log in to join the discussion.
Loading…