I'm attempting to create a function that finds how many times an item occurs in a function. I have the code below, but it ends up counting all the items in the list(6), not just the one i want( in this case 1). I know i'm not returning anything, the print is mainly for me to see what answer I get.
def count(sequence, item):
found = 0
for i in sequence:
if item in sequence:
found = found + 1
print found
count([1,2,1,2,4,5], 1)
if item == i. printialso on each iteration. You probably will understand why you should check againstithat way.itemis always insequence. So for every iteration, your code gets intoifand incrementsfound.