Count


#1


https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/1?curriculum_id=4f89dab3d788890003000096

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


#2

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


#3

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


#4

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.


#5

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


#6

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