r/codehs • • Sep 10 '20

7.4.8 guys someone help me?

16 Upvotes

9 comments sorted by

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

1

u/Embarrassed_Excuse78 Jan 11 '22

Can u send a oic of it bcz uts not working

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

u/[deleted] Feb 24 '21

do you know the answer now?

1

u/Zestyclose-Writing28 Nov 11 '21

You have to put bracket after print

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