CBSE Class 11 Computer Science Question 92 of 112

Dictionaries — Question 14

Back to all questions
14
Question

Question 11(a)

Find the errors:

text = "abracadbra"
counts = {}
for word in text :
  counts[word] = counts[word] + 1
Answer
Output
KeyError: 'a' 
Explanation

The line counts[word] = counts[word] + 1 will cause a KeyError because we are trying to access a key word of an empty dictionary. As the key is not present in dictionary counts, hence a KeyError is generated.