+ 2
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} text = input() dict = {} #your code goes here for letter in text : key =str (letter ) value = text .count(letter ) print ({key : value }) Plz help🤪
8 Respuestas
+ 11
Try this
text = input()
dict = {}
#your code goes here
for char in text:
counts = text.count(char)
dict[char] = counts
print(dict)
+ 7
You've almost got it, but your code only prints the last key: value pair created. You need to create your dictionary, pair by pair, then print it.
The easiest way is using a comprehension like this (using set() eliminates duplicates):
letters = {letter: text.count(letter) for letter in set(text)}
print(letters)
Here's an example
https://code.sololearn.com/c5SZz6Ecsnqz
+ 4
# the most efficient (and shortest) way would be to use Counter object from collections module:
from collections import Counter
print(dict(Counter(text)))
+ 2
Thank u all😇
+ 1
text = input()
dict = {}
count = 0
for i in text:
count = text.count(i)
dict[i] = count
print(dict)
+ 1
def letter_count(text, letter):
#your code goes here
return text.count(letter)
text = input()
letter = input()
print(letter_count(text, letter))
0
Can you tell what output u want give more details about Question u have posted code only
0
David Ashton why does it print only the last pair?