0

How to count number of letters in string.....for example input = hello and output = {'h':1,'e':1,'l':2,'o':1} in python

Help me with this pls --------------------------------------------------------------- Full Question: 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} --------------------------------------------------------------- My code: text = input() empty_dict = {} ab = collections.Counter(text) for key,value in ab.items(): if value>0: repeated[key] = value print(repeated)

27th May 2022, 2:19 PM
Avi
Avi - avatar
4 ответов
+ 2
Have you ever run your code yourself? Go to Code section, click +, select the programming language, insert the code, run. Read the error message. You need to import a module before you can use it. repeated is not defined. People are more likely to help if you put your code in a script on sololearn playground than when you paste it into the description.
27th May 2022, 2:26 PM
Lisa
Lisa - avatar
+ 1
READ MY REPLY CAREFULLY: You need to import a module before you can use it.
27th May 2022, 2:30 PM
Lisa
Lisa - avatar
0
Lisa actually it was saying collections not defined.....
27th May 2022, 2:29 PM
Avi
Avi - avatar
0
Lisa ohh! Thanks I forgot that xD🙂✌️
27th May 2022, 3:20 PM
Avi
Avi - avatar