 [SOLVED] What am I doing wrong - letter counter exercise in intermediate python | Sololearn: Learn to code for FREE!

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

+53

# 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())))

+28

Try my simple solution! text = input() dict = {} for t in text: dict[t] = text.count(t) print(dict)

+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

+9

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

+8

text = input() dict = {x: text.count(x) for x in text} print(dict)

+4

text = input("> ") dict = {} for letter in text: dict.update({letter: text.count(letter)}) print(dict) very similar to jacky lei code

+3

text = input() dict = {} count = 0 for x in text: count += 1 dict[x] = text.count(x) Print(dict)

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

+1

text = input() dict = {} for t in text: dict[t] = text.count(t) print(dict) This is Correct answer

+1

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)

0

Abhay and visph - thank you!

0

text = input() dict = {} #your code goes here for i in text: dict[i] = text.count(i) print(dict)

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)

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

0

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

0

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

0

text = input() dict = {} for i in text: dict[i]=text.count(i) print(dict) this is the simplest solution i could think of.

0

My : text = input() dict = {} #your code goes here for x in text: dict[x] = text.count(x) print(dict) !!! Try before !!!

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

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