- 10

[SOLVED] Letter Counter Intermediate Python

I'm stuck where the dictionary will only count one value for each letter, I would appreciate being pointed in the right direction on where to go from here.. text = input() dict = {} for x in text: dict[x] = x.count(x) if x in dict: dict[x] += 1 print(dict) Edit: solved by moving my if statement to the start of the for loop 👀

7th Jun 2021, 11:21 AM
Bianca S
Bianca S - avatar
21 Answers
+ 10
see this code, I am checking that If keys or letters are avalible then just update the value by 1 , else make a key of name that letter and make it's values 1 https://code.sololearn.com/c4B7Zb7voT80/?ref=app
7th Jun 2021, 11:29 AM
Abhiyantā - avatar
+ 24
Here is what i did ,it works(it would help) text = input() dict = {} #your code goes here for i in text: if i not in dict: dict[i]=1 else: dict[i]+=1 print(dict)
19th Sep 2021, 1:49 PM
Hasnae BOUHMADY - avatar
+ 13
Bianca S You could also use dictionary comprehension here: a = input() print({x:a.count(x) for x in set(a)}) # Hope this helps
9th Jun 2021, 6:20 AM
Calvin Thomas
Calvin Thomas - avatar
+ 4
Try this code I tried it using dictionary comprehension text = input() dic = {I: text.count(i) for i in text } print(dic)
8th Jun 2021, 9:57 PM
Victoria - avatar
+ 4
Hasnae Bouhmady your solution is the best !
21st Nov 2021, 9:07 PM
Tugba Ceren Turhan
Tugba Ceren Turhan - avatar
+ 2
""" you could also use the built-in Counter class from 'collections' module, wich return a dict-like, wich can be easily converted to a real dict: """ from collections import Counter text = input() print(dict(Counter(text))) # https://pymotw.com/2/collections/counter.html
7th Jun 2021, 11:04 PM
visph - avatar
+ 2
What do dict={} do?
18th Sep 2021, 8:54 AM
Saad Khan
Saad Khan - avatar
+ 2
@saad Khan dict={} is an empty dictionary ,you need to declare it first and then you can fill in it whith keys and values
19th Sep 2021, 1:48 PM
Hasnae BOUHMADY - avatar
+ 2
Calvin Thomas technically your code is doing what is wanted but they want in order according to order of input letters so that's why it doesn't really works.
21st Nov 2021, 8:59 PM
Tugba Ceren Turhan
Tugba Ceren Turhan - avatar
+ 2
# These three solutions for help: #solving number 1: text = input() #your code goes here from collections import Counter c = Counter(text) print(dict(c)) #solving number 2: word = input('Enter word: ') d = dict() for i in word: d[i] = word.count(i) print(d) #solving number 3: word = input('Enter word: ') print({letter: word.count(letter) for letter in word})
12th Dec 2021, 3:16 PM
Mohammad Jamal Mahmoud Al Jadallah
Mohammad Jamal Mahmoud Al Jadallah - avatar
+ 1
11th Aug 2021, 12:10 PM
Eric C Miller
+ 1
Here is what I did text = input() dict = {} #your code goes here for i in text: w = 1 if i in dict: w = text.count(i) dict [i] = w else: dict[i] = w print (dict)
7th Jul 2022, 12:07 PM
+ 1
'Letter Counter' '''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} You need to output the dictionary object. Note, that the letters are in the order of appearance in the string.''' text = input() dict1 = {} #your code goes here for i in text: if i not in dict1: dict1[i]=1 else: dict1[i]+=1 print(dict1)
19th Oct 2022, 1:00 PM
Yurchenko Georgiy
Yurchenko Georgiy - avatar
+ 1
Ahh, it's Ez guys-- #letterCounter text = input() dict = {} for i in text: dict[i] = text.count(i) print(dict)
7th Nov 2022, 11:27 AM
Nishanth C
Nishanth C - avatar
+ 1
text = input() dictionary = {} keys = dictionary.keys() for letter in text: if letter not in keys: dictionary[letter] = 1 else: dictionary[letter] = dictionary[letter] + 1 print(dictionary)
28th Nov 2022, 11:46 PM
+ 1
my solution; text = input() dict = {} letter=[] for i in text: dict.update({i:text.count(i)}) print(dict)
25th Jan 2023, 2:16 PM
text = input() dict = {} for x in text: dict[x]=text.count(x) print(dict) #your code goes here
27th May 2022, 6:20 AM
Aditya Puri
Aditya Puri - avatar
If you want to use count you should use a function to return the dict this is for modifying code
4th Aug 2022, 10:30 PM
Darya - avatar
My solution text = input() dict = {} for x in text: if x in dict: dict[x]+=1 dict[x]= text.count(x) print(dict)
18th Dec 2022, 11:01 PM
Fouaz Laadjel
Fouaz Laadjel - avatar
This worked for me. Seems simple enough. for letter in text: dict[letter] = text.count(letter) print(dict)
20th Dec 2022, 6:44 PM
Plamen Petkov
Plamen Petkov - avatar