11. count


#1


Oops, try again. Your function crashed on ([1], 7) as input because your function throws a "argument of type 'int' is not iterable" error.

Is there something wrong with my code?


def count(sequence, item):
    lst = []
    total = 0
    
    for something in sequence:
        lst.append(something)
    else:
        for element in lst:
            if element in item:
                total += 1
    return total
    
print count(your_sequence, your_item)


#2

The error message from codecademy tells you how you can reproduce the crash.

Reproduce that crash, and take note of where python's error message says it happened. Also take note of what it's saying can't be done - because that's what you were attempting to do.

Then consider what you were doing there, reconsider what you wrote (make sure you can argue for why it would do what you want)
If you don't know how to fix that, then google for the action you were trying to carry out in that location, or ask about that action.

Also, I suspect that you can't motivate what your else is doing - and if you can't motivate what it does, then you can't have it in your code because you need to know what each thing in your code does. (Anything else would be like picking random words from a word list to write a story. You won't be in control of what that story is)


#3

I already dealt with it by myself. I am not a native English speaker, so I just could not interpret well what was exactly requested by codecademy.
Anyway thanks for your comment

def count(sequence, item):
    lst = []
    total = 0

    for something in sequence:
        lst.append(something)
    for splited in lst:
        if splited == item:
            total += 1
    return total

print count(your_sequence, your_item)

#4

Is there any reason why you need lst? Could you use sequence instead, and completely remove lst?

Also, an easier way to create a list from an iterable (when the list type is called with an argument, it iterates through the argument and creates a list containing each element that was iterated through):

copy = list(sequence)

Or if it's a sequence (indexable and has a length), then it can also be sliced from beginning to end (begin and end are specified before and after the colon, but defaults are used when they are left out):

copy = sequence[:]


#5

Oh I didn't know these useful tequniques... I tried and it makes my code smarter, thanks!


#6

You won't need it in your code though. Because you don't need to create a copy. Just use the original.


#8

Why did you variable format?


#9

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