+ 2

Hi everyone

This is my question in python Given a string as input, you need to output how many times each letter appears in the string. You decide to store the data in a dictionary, with the letters as the keys, and the corresponding counts as the values. Create a program to take a string as input and output a dictionary, which represents the letter count. Sample Input hello Sample Output {'h': 1, 'e': 1, 'l': 2, 'o': 1} And I answer like this text = input() dict ={} #your code goes here for e in text: dict [e]= text.counts(e) print(dict) After run I have a string attributed error How can I fix it?

12th May 2022, 4:08 AM
Sharareh.H
Sharareh.H - avatar
3 odpowiedzi
+ 4
Sharareh heyati you added an 's' in count. text.counts(e) it should be : text = input() dict ={} for e in text: dict[e] = text.count(e) print(dict)
12th May 2022, 4:30 AM
Bob_Li
Bob_Li - avatar
+ 1
ThanksBob_Li 😬👌
12th May 2022, 6:19 AM
Sharareh.H
Sharareh.H - avatar
0
oh
19th Aug 2023, 8:31 PM
Nimona Getachew
Nimona Getachew - avatar