Count


#1


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

I am trying to create handling for floats. Currently I get the greenlight with this code and I pass, but it doesn't count floats that end in 0. For example the current code returns 2 and I want it to return 5.

I expect all the numbers with a float of .0 to still increase the counter.

def count(sequence, item):
    found = 0
    
    for i in range(len(sequence)): # loop through the list, item by item 
        if str(sequence[i]) == str(item): #check if i matches item
            found += 1    # if True, return found plus 1  
    return found
print count([69.0,"69.0",1,69.0, "69", 69, 69.0654326], 69)


#2

look:

def count(sequence, item):
    found = 0
    
    for i in range(len(sequence)): # loop through the list, item by item 
        print str(sequence[i]), str(item)
        if str(sequence[i]) == str(item): #check if i matches item
            found += 1    # if True, return found plus 1  
    return found
print count([69.0,"69.0",1,69.0, "69", 69, 69.0654326], 69)

even casting to string, 69 still won't equal 69.0

you could add an or operator to your if statement:

str(sequence[i]) == str(item) + ".0"

not a great solution, but its something


#3

Yes, I realized that, but I couldn't pass the lesson until I put the str() method in front. After several hours of reading about int() and float() I tried converting to float which would return the correct number, but the program wouldn't give me the green light to go onto the next lesson.


#4

really, I was just confused as to why int(sequence[i]) == int(item) wouldn't work. If you could answer that I would be very happy :+1:


#5

maybe its good to quickly reflect on data types:

"i am a string" # string
"3" # string
"5.2" # string
3 # integer
5.2 # float
str(3) # integer converted to string
str(5.2) # float converted to string
int("3") # string converted to integer
float("5.2") # string converted to float

so far so good?

the problem with the lesson is that it should handle:

count(["one", "one", "two", "three", "five"], "one")

and more.

i think then adding the or case like i suggested seems the best approach


#6

okay thanks for the info


#7

did you figure out the or condition i suggested?

if str(sequence[i]) == str(item) or str(sequence[i]) == str(item) + ".0":

#8

Ya that makes sense, but then I would have to code in handling for the digits 1 - 9 as well.


#9

oh, where did you say that? I missed that. Then you should handle a very different stratergy

what cases should your function be able to handle?


#10

I just wanted it to be robust enough to take any list of numbers (whether their data type be a string, integer, or float) and then compare it to the designated number in the argument called 'item' to see if it was a match or not. But I feel I am probably over complicating it.


#11

but it currently looks like you also want floats to equal integers?


#12

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