Letter Counter Python | Sololearn: Learn to code for FREE!

+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)

9/18/2021 1:10:04 PM

MartiiOliwa

9 Answers

New Answer

0

MartiiOliwa blocks are the problem Try to use print statement outside loops

+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)

+1

Thank you HrCoder ! 😊

+1

MartiiOliwa I have removed unnecessary parts from your code Check it text = input() dict = {} for x in list(text): dict[x] = dict.get(x, 0) + 1 print(dict)

0

can you please tell me what this means dict[x]

0

thanks bro