Computer Science

Find the errors:

text = "abracadbra"
counts = {}
for word in text :
  counts[word] = counts[word] + 1

Python Dictionaries

9 Likes

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.

Answered By

3 Likes


Related Questions