Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2
im not sure which challenge or problem this is for, but my attempt would be something like this: words = input() letters = {} for letter in words: if letter not in letters.keys(): letters[letter] = 1 else: letters[letter] = letters[letter] + 1 for key in letters.keys(): print(f'{key}: {letters[key]}')
17th Mar 2021, 2:29 AM
you are smart. you are brave.
you are smart. you are brave. - avatar
0
You should use a loop to iterate over all the characters of the text. Your code only checks only if letter is in text or not. So either the function returns 1 or None when there's no letter in the text. Use a for loop like this: total = 0 for char in text: if char == letter: total += 1 return total
17th Mar 2021, 1:43 AM
Abir Hasan
Abir Hasan - avatar
0
def letter_count(text, letter): return len([i for i in text if i == letter]) Or, as Wilbur Jaywright suggested the return statement could just be return text.count(letter)
17th Mar 2021, 5:50 AM
David Ashton
David Ashton - avatar
0
just bring back return in the loop my try:def letter_count(text, letter): count=0 for i in text: if i== letter: count= count+1 return count text = str(input()) letter = input() print(letter_count(text, letter))
16th Jan 2022, 5:41 PM
Mahshid Shaaker
- 2
Short answer: you forgot to loop! the if statement only gets called once, so 1 or 0 is printed. PS: u can cheat and use the count() method.
17th Mar 2021, 3:04 AM
Wilbur Jaywright
Wilbur Jaywright - avatar