+ 1

[SOLVED] What am I doing wrong - letter counter exercise in intermediate python

**This is the exercise:** 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} **Here's my code:** text = input() dict = {} count = 0 for x in text: count += 1 dict[x] = count print(dict) But it's not counting how many times each letter appears, it's saying where in the string the last instance of each letter appears. How do I fix this? (Not looking for other solutions, I've already found some great ones - such as this https://code.sololearn.com/cZw2842qky59/?ref=app - just wanting to work through this approach for my own understanding.)

19th Feb 2021, 10:26 PM
Zoë
Zoë - avatar
39 Answers
+ 58
# without module import and use of implicit nested loops implied by call to count() method: text = input() dict = {} for char in text: if char in dict: dict[char] += 1 else: dict[char] = 1 print(dict) # shortest and most efficient (most pythonic) solution would be to use built in module collections: from collections import Counter print(dict(Counter(input())))
19th Feb 2021, 10:55 PM
visph
visph - avatar
+ 27
Try my simple solution! text = input() dict = {} for t in text: dict[t] = text.count(t) print(dict)
19th Apr 2021, 12:02 AM
Karl Joseph Buenafe
Karl Joseph Buenafe - avatar
+ 17
In your code ,count is incremented on every iteration over text , so basically you end up with dictionary as last position of each letter in text . What you should do is check if letter exists as key in dictionary or not . =>If it exists update the value for that key by 1 . =>if it doesn't exists assign 1 to your new key . eg. for x in text: if x in dict: dict[x]+=1 else: dict[x]=1
19th Feb 2021, 10:40 PM
Abhay
Abhay - avatar
+ 9
text = input() dict = {} for i in text.lower(): dict[i] = text.count(i) print(dict)
9th Mar 2021, 12:15 AM
Jacky Lei
Jacky Lei - avatar
+ 9
text = input() dict = {x: text.count(x) for x in text} print(dict)
15th Jun 2021, 5:45 PM
Andrey
+ 4
text = input("> ") dict = {} for letter in text: dict.update({letter: text.count(letter)}) print(dict) very similar to jacky lei code
12th Apr 2021, 9:07 PM
i Cased
i Cased - avatar
+ 3
text = input() dict = {} count = 0 for x in text: count += 1 dict[x] = text.count(x) Print(dict)
4th Jun 2021, 5:27 PM
Sunidhi Soni
Sunidhi Soni - avatar
+ 2
def letter_count(text, letter): counters = 0 for char in text: if (char == letter): counters = counters +1 return counters #your code goes here text = input() letter = input() print(letter_count(text, letter))
7th Jun 2021, 9:22 AM
Eduards
+ 2
Check this out- This works text = input() dict = {} #your code goes here for n in text: keys = dict.keys() if n in keys: dict[n] += 1 else: dict[n] = 1 print(dict)
9th Jan 2022, 2:16 PM
Arjun Vijay Prakash
Arjun Vijay Prakash - avatar
+ 1
text = input() dict = {} for t in text: dict[t] = text.count(t) print(dict) This is Correct answer
29th Jun 2021, 5:54 PM
Budhabhushan Waghmare
Budhabhushan Waghmare - avatar
0
Abhay and visph - thank you!
20th Feb 2021, 7:30 AM
Zoë
Zoë - avatar
0
text = input() dict = {} #your code goes here for i in text: dict[i] = text.count(i) print(dict)
23rd May 2021, 9:08 AM
Orutwa Justine N.
Orutwa Justine N. - avatar
0
Another way to go for it: text = input() dict = {} #your code goes here for character in text: if character.isalpha(): dict[character] = text.count(character) text.replace(character,"") print(dict)
27th Jul 2021, 8:16 PM
Mwaniki Grace Waigumo
Mwaniki Grace Waigumo - avatar
0
text = input() dict = {} for x in text: if x in dict: dict[x] += 1 else: dict[x] = 1 print(dict) print(dict(Counter(input()))) #letter_count #python
28th Jul 2021, 4:24 PM
Med Brb
Med Brb - avatar
0
text = input() dict = {} for i in text.lower(): dict[i] = text.count(i) print(dict)
15th Aug 2021, 9:26 AM
Vraj Soni
Vraj Soni - avatar
0
text = input() dict = {} for char in text: if char in dict: dict[char] += 1 else: dict[char] = 1 print(dict)
15th Aug 2021, 12:09 PM
Sahana ca
Sahana ca - avatar
0
text = input() dict = {} for i in text: dict[i]=text.count(i) print(dict) this is the simplest solution i could think of.
18th Aug 2021, 8:31 AM
Nikita Agarwal
Nikita Agarwal - avatar
0
My : text = input() dict = {} #your code goes here for x in text: dict[x] = text.count(x) print(dict) !!! Try before !!!
26th Aug 2021, 10:25 AM
Rayen Boussayed Mohammed Amin
Rayen Boussayed Mohammed Amin - avatar
0
correct answer is def letter_count(text, letter): counters = 0 for char in text: if (char == letter): counters = counters +1 return counters #your code goes here text = input() letter = input() print(letter_count(text, letter))
26th Aug 2021, 10:25 PM
Shan Romesh
Shan Romesh - avatar
0
Use code: text = input() dict = {} for char in text: if char in dict: dict[char] += 1 else: dict[char] = 1 print(dict) input:- hello
24th Sep 2021, 4:03 PM
Adi Vaidya