Count need some final help


I am able to get through a number of different tests with this code. However, I fail when a float is introduced. Any helpful pointers in the right direction would be appreciated.

Here is my code.

def count(sequence, item):
a = str(item)
b = str(sequence)
for item in sequence:
c = b.count(a)
return int(c)


The only operation you need to perform on the values is to test if two of them are equal

Why would floats present a problem for that operation? Floats support the == operator.

If you're supposed to implement count, wouldn't using a method called count mean that you're avoiding implementing count?


My issues is I have a sequence as follows [ 7.8, 4, [], fluffy, 7] and the item being tested is 7.

my count operator returns 2 instead of 1. because it is catching the 7 in 7.8.

I am not sure what you mean by using count is somehow avoiding count. Count is catching the number of times the item is found in the list, and at the end i return the number converted to an integer.


Well why would your code think they match?

7.8 == 7 evaluates to false, I see no problem there

You're using a method named count that already has the behaviour that you are supposed to implement. So you're .. avoiding it and instead letting that do the work. If you were supposed to implement addition, then using the + operator would be defeating the exercise as there would be nothing left to do, + already implements the thing that was supposed to be implemented and that removes/avoids the exercise. It's like having the dog go to the gym for you.


I understand what your saying i am not supposed to use the count action in this problem and will rework it.

However, for some reason it was evaluating 7.8==7 as true. I am assuming because i converted both items to a string. there for it searched "7.8" for a "7" and found one. Very much like it would search the word "apple" for an "a" and find one. even though a != apple


Was it? I don't think you're doing that at all. So the question then is what you're doing. But if you're supposed to count how many are equal to some value, then you gotta test if they are equal

Yeah well.. that doesn't exactly match the task description does it? You were supposed to water the plants but instead you re-planted them..


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