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]}')
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
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)
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))
- 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.