 LETTER COUNTER | Sololearn: Learn to code for FREE!

+4

# LETTER COUNTER

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} Please help me to solve this problem.

+10

Show your attempt first. Then we'll help you to correct it.

+9

P.SAI SRI RAM - CODE HERO Check this one out. https://code.sololearn.com/cRMwJkdnCHU5/?ref=app

+7

P.SAI SRI RAM - CODE HERO Try this https://code.sololearn.com/cebXGf11Gq58/?ref=app

+6

text = input() dict = {} for char in text: counts = text.count(char) dict[char] = counts print(dict)

+4

Don't forget about return in the function

+3

You can try to revise this: https://www.sololearn.com/learn/Python/2457/

+3

K.S.S. KARUNARATHNE you are the best!

+3

https://code.sololearn.com/c6T30ieQcUx5 Add pairs to the dictionary using the "get(key [, default])" function, which looks up the key [i] (the letters at, which you want to add). If it is not, adds a pair, key = letter [i] with a default value = 0 (instead of "None" so that you can do mathematical operations) and append to the value 1 (+1). If the key is already in the dictionary, just adds 1 (+1) to the value. >>> text = input() dict = {} for i in text: dict[i] = dict.get(i,0) + 1 print(dict) Another way: >>> text = input() dict = {a:b for a in text for b in str(text.count(a))} print(dict)

+2

Try this text = input() dict = {} for letter in text: dict.__setitem__(letter,text.count(letter)) print(dict)

+2

+1

I was completely stumped on this project as well, thanks!

0

text = input() dict = {} #your code goes here def dicting(text) for char in text: if char not in text: dict += ch

0

text = input() dict = {} for char in text: if char not in dict: dict[char]=1 else: dict[char] += 1 print(dict)

0

text = input() dict = {} for x in text: dict[x] = dict.get(x,0) if x in dict: dict[x] += 1 print(dict)