Q. Find the error in following code. State the reason of the error.
alst ={ 'a':1, 'b':2, 'c':3}
print (alst['a', 'b']).
Answer :-
The above code will produce KeyError, the reason being that there is no key same as the list ['a', 'b'] in dictionary alst. It seems that the above code intends to print the values of two keys 'a' and 'b', thus we can modify the above code to perform this as
alst={'a':1, 'b':2, 'c':3}
print (alst['a'], alst['b'])
Now it will give the result as :
1 2
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )