My code says File "python", line 5 SyntaxError: 'return' outside function and it says
Oops, try again. Did you create a function called count? Your code threw a "global name 'count' is not defined" error.

 def count(sequence,item):
        found = 0
for item in sequence:
        found + 1
return found

Thanks in advance


Sorry the thanks in advance was not meant to be in my code when writing this post i put it there by accident


take a look on your for-statement and the return-statement - and check the indention!

just a hint: you want to increment found for every item in sequence, but you have to assign the new value to found instead of just adding 1


The function is to return the number of times the item occurs in the sequence.

The for-loop and the return statement should go inside the function block.

Use an if statement to see if an element in the sequence is equal to the item.

for elem in sequence:
    if elem == item:
        found += 1   # same as found = found + 1

As you see, you are using the same names for your function parameter (item) and for-loop identifier (item). They should be different.


Thanks for the reply it was silly of me to forget to put the = there


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.