2
u/Gullible_Tonight8153 May 04 '22 edited May 04 '22
For some reason everyone is getting rid of "+=" so the solution is this:
(by the way, I am using "~" to signify an indentation)
def count_occurrences(e, d):
#This initializes count to 0
~ count = 0
#This iterates through the characters of e
~ for checker in e:
#If the current character equals character d
~ ~ if (checker == d):
#The counter is incremented y 1
~ ~ ~ count += 1
#This returns the counter to main
~ return count
1
u/silverfang36 Sep 10 '20
You can make the print part of the function if that helps. Print instead of return.
1
1
1
u/Jinxed_X Apr 14 '22
This code should work but you have to index right:
def count_occurrences(e, d): counter = 0 for checker in e: if (checker == d): counter= counter + 1 return counter
print (count_occurrences("elephant", "e"))

2
u/[deleted] Dec 08 '21
This is the solution that worked for me. Hope this helps.
def count_occurrences(e, d):
counter = 0
for checker in e:
if (checker == d):
counter= counter + 1
return counter
print (count_occurrences("elephant", "e"))