Letter Counter Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Letter Counter Python

Can anyone help me with this code? Why doesn't it work? text = input() dict = {} #your code goes here for letters in text: letters = letters.split() for x in letters: dict[x] = dict.get(x, 0) + 1 print(dict)

18th Sep 2021, 1:10 PM
MartiiOliwa
MartiiOliwa - avatar
5 Answers
0
MartiiOliwa blocks are the problem Try to use print statement outside loops
18th Sep 2021, 1:43 PM
Pariket Thakur
Pariket Thakur - avatar
+ 5
(1) it is not necessary to convert the input text to a list. string can be iterated directly. (2)if we want to count all characters except spaces, we can remove them directly with input().replace(...) # (1) text = input() dic = {} for x in text: dic[x] = dic.get(x, 0) + 1 print(dic) # (2) text = input().replace(" ","") dic = {} for x in text: dic[x] = dic.get(x, 0) + 1 print(dic)
18th Sep 2021, 3:51 PM
Lothar
Lothar - avatar
+ 1
Thank you HrCoder ! 😊
18th Sep 2021, 1:52 PM
MartiiOliwa
MartiiOliwa - avatar
+ 1
thanks bro
8th Oct 2021, 3:47 PM
AjwadRehman
AjwadRehman - avatar
0
can you please tell me what this means dict[x]
5th Oct 2021, 5:04 PM
AjwadRehman
AjwadRehman - avatar