Count. Seems like the problem at your side


#1



count function

My code works correctly but Codecademy reports the error "Oops, try again. Your function fails on count([4, 'foo', 'foo'],5). It returns 1 when it should return 0."

It is not truth:

I can rewrite code but why should I do this? May be need to improve Codecademy? :slight_smile:


My code:

def count(sequence, item):
    times = 0
    while item in sequence:
        sequence.remove(item)
        times += 1
    return times
print count([4, 'foo', 'foo'], 5)


#2

I think instead of a while loop:

You would be better off using a for each loop. The syntax for this is

for item in sequence:
    do some stuff

The only reason this is beneficial is to iterate through a list, because that's exactly what for each loops do. Then inside of the for in loop, you need an if statement that says if the number in sequence is equal to item, times += 1.
Lastly, you do not need to call the the function count, but it's okay if you do.
I hope this helps! :slight_smile: If you have any other questions, let me know.


#3

Thank you for answer.
I wrote:

But I want to correct a proofing system of Codecademy. When code returns 0 and learning system says: "it returns 1" this is incorrectly.


#4

A wild bug appears ...

Obviously, there is a bug in Codecademy's submission correctness test (SCT) for this exercise. The message makes no sense, therefore an explanation is in order.

Codecademy assumes that the user's count function will not make any changes to the list that is passed to it. However, the instructions do not ask that we write the function in a manner that leaves the original list intact.

When a mutable object, such as a list, is passed to a function as an argument, that argument and the corresponding function parameter wind up referring to the same object, rather than a copy of the object. Therefore, if the function makes any changes to the object, it is making the same changes to the original object.

We can experiment with your count function, @voropayev , by adding the following lines after the function definition ...

seq = [4,"foo",5,"foo"]
print seq
print count(seq, 5)
print seq
print count(seq, 5)

Output ...

[4, 'foo', 5, 'foo']
1
[4, 'foo', 'foo']
0

The output demonstrates that the function altered the original list.

Codecademy tests the user's count function by passing it several sets of test data. One of the tests involves counting the occurrence of 5 in the list, [4, 'foo', 5, 'foo'], as above. @voropayev's function returns 1, as it should, but then, as we can see, the list has been changed to [4, 'foo', 'foo']. Next, Codecademy uses its own version of the count function to look for 5 in the list. Since the 5 is now gone from the list, it comes back with a count of 0. Since the 1 that was returned originally does not match the 0 returned by Codecademy's function, Codecademy fires off an error message. Built into the error message is the most recent content of the list, now [4, 'foo', 'foo'], the 1 that was returned by @voropayev's count function, and the 0 returned by Codecademy's own version of count.

That is the explanation for the wild bug that appeared.

Though Codecademy did not specify that the count function should leave the list intact, it is good programming practice to have a function leave objects intact unless the purpose of the function is to alter the object in a specific manner. In the case of this exercise, the instructions and the SCT should be revised.


#5

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