I’m trying to create a program where if you input a word, it will print out each letter of the word and how many times the letter appears in that word.
Eg; when I input “aaaarggh”, the output should be “a 4 r 1 g 2 h 1”.
def compressed (word): count = 0 index = 0 while index < len(word): letter = word[index] for letter in word: index = index + 1 count = count + 1 print(letter, count) break print("Enter a word:") word = input() compressed(word)
So far it just prints out each letter and position in the word. Any help appreciated, thank you!
(no using dict method)
Advertisement
Answer
a="aaaarggh" d={} for char in set(a): d[char]=a.count(char) print(d)
output
{'a': 4, 'h': 1, 'r': 1, 'g': 2}