Python - Practice makes Perfect - Question regarding count exercise


#1

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

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?


#2

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):
    CENSORED
    # boolean value
    item_is_list = isinstance(item, (tuple, list))

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

#3

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.


#4

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.


#5

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


#6

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.


#7

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:


#8

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


#9

Haha I tried that at first but I got this:

So I removed the assignment :stuck_out_tongue:

Any more tips?


#10

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


#11

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!


#12

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:.


#14