0
Letter counter in python, problem! Why returns 0 value? Help me!
def letter_count(text, letter): count = 0 for x in text: if x == letter: count += 1 return count text = input() letter = input() print(letter_count(text,letter))
3 Answers
+ 3
def letter_count(text, letter):
count = 0
for x in text:
if x == letter:
count += 1
return count
text = input()
letter = input()
print(letter_count(text,letter))
#or
print(text.count(letter))
+ 3
Indentation is important in Python. Your return statement is indented inside the for loop, making the program exit early the first count value (0 in your case). To fix, reduce the indent level by one.
def letter_count(text, letter):
count = 0
for x in text:
if x == letter:
count += 1
return count # return after loop finishes
text = input()
letter = input()
print(letter_count(text,letter))
+ 2
Thanks! đđđ