Suppose I have the following sentence:
oracion = 'Mi perro bonito come todas las noches'
And I want to create a function that tells the occurrence of each character within it. To that end, we import string and create an alphabet that is our reference:
import string
alphabet = string.ascii_letters
The function I did for the word counter is:
def contador(str):
count= {}
for letter in str:
if letter in alphabet:
if letter in count.keys():
count[letter]+=1
else:
count[letter]=1
return count
When I run
contador(oracion)
I always get this exit:
Out[1]: {' ': 1}
That is, there is no dictionary that registers the frequency of each letter within the sentence. Could you tell me what the source of the error is? Honestly my knowledge has not given me yet to find out what I'm wrong about, since I think the reasoning behind the function is correct. Thanks in advance.