149k views
5 votes
You created the following dictionary relationships = {'Jimmy':'brother', 'Carol':'sister'}. You then executed the following code, and received a KeyError exception. What is the reason for the exception? relationships['jimmy']

1 Answer

2 votes

Answer:

This is because the key in relationships['jimmy'] is wrong. The first letter "j" should be uppercase. The key in the given dictionary is "Jimmy" and therefore a lowercase "j" will result in a KeyError exception. The key is case sensitive and therefore a minor mistake in a single letter either in lowercase or uppercase will cause the error. The correct usage of key to address the value should be relationships["Jimmy"].

User Francesco Papagno
by
3.4k points