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))

12th Nov 2022, 7:34 AM
Zafarjon Obidov
Zafarjon Obidov - avatar
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))
12th Nov 2022, 7:51 AM
SoloProg
SoloProg - avatar
+ 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))
12th Nov 2022, 7:57 AM
Mozzy
Mozzy - avatar
+ 2
Thanks! šŸ˜ƒšŸ˜ƒšŸ˜ƒ
12th Nov 2022, 8:08 AM
Zafarjon Obidov
Zafarjon Obidov - avatar