Python - Practice makes Perfect - Question regarding count exercise
<In what way does your code behave incorrectly? Include ALL error messages.>
Although I got through this exercise, I have a question regarding what is termed by some users as the shortest accepted solution for this exercise. The proposed solution is:

def count(sequence, item):
    amount = 0
    for i in sequence:
        if i == item:
            amount += 1
    return amount

However, I feel that this will pose a problem: not being able to take a list as input

According to the instructions, “The item you input may be an integer, string, float, or even another list!”. However, going by this proposed code, the part about the input being able to take on a list cannot be realised; the console will just return 0 even when you print the function after it is called:

In this case, why is this solution still accepted since it does not fulfill all the requirements?

1 Like

I would congratulate you for extremely keen eyes and ability to read and implement the instructions perfectly! :smiley: Well done! :tada:

Unfortunately, though we recognise this is a bug, however as it does not prevent users from passing the lesson and this is one of the older courses it is unlikely this bug will be fixed. Thank you for taking the time to inform us though, we would like everything to be fixed just as much as you would!

Side note: the fix for this problem is very simple:

using isinstance method
more on tuples here

def count(sequence, item):
    # boolean value
    item_is_list = isinstance(item, (tuple, list))

    for CENSORED
        if i == item:
        # this is the condition you need to concentrate on
        elif item_is_list and i in item: # use in operator instead of ==
            c += 1
print count([1, 2, 3, 1, 4, 1], [1, 2]) # 4
1 Like

I did not see any problem although I did it completely different way.And I used logic and syntax which we learnt within this curriculum not anything else.

I agree with you, since the exercise SCT is NOT checking if item is a list, the requirement for item to be able to be even a list (instructions - “The item you input may be an integer, string, float, or even another list!”) should be dropped altogether. Checking if item is a list requires to use methods which haven’t yet been taught.

@umopapsidn @gaurangtandon Thanks both of you, I see your point.

Just to give you update, I think it can be done with materials taught in the curriculum before this exercise. I think SCT is checking if it is a list or not. By the way, My code accepts all the input mentioned in the exercise without changing the code.

Hi @mashuk,

I am actually quite interested in your solution. Unfortunately it seems like your notepad has covered the most essential bits, causing me to be unable to see anything new. Can I request you to repost your code? :slight_smile:

I actually gave it another shot myself, but I am not too sure why it failed:

You seem to be going in the right direction. Remember to reassign the result of str(item) to item :wink:

Haha I tried that at first but I got this:

So I removed the assignment :stuck_out_tongue:

Any more tips?

Also, your if condition is wrong. Visit this FAQ post (it’s in JS, but you get the idea) for more info.

1 Like

Ah I see…that’s a helpful reminder, thanks a lot for that :slight_smile:

But even so, I will still get this:

Not sure if it is worth the trouble to pursue this all the way, but I’d hate to give up midway :stuck_out_tongue:

Thanks a lot!

Ah, I see the problem. I am not acquainted with Python really much, so I didn’t anticipate this problem beforehand. Seems like the in operator only likes to work when its left operand is a string.

Simple solution is to do this instead: if str(i) in item:.