Count - str error


#1



When I run the code below, I get the proper number response (it returns "1") for my sequence and Item, but I'm having an issue figuring out the error below.


ERROR:
Oops, try again. Does your count function take exactly two arguments: a list and an 
item to check against that list? Your code threw a "unsupported operand 
type(s) for /: 'str' and 'str'" error.


def count(sequence, item):
    total = 0
    for i in sequence:
        if item == i: 
            total += i/i
    return total
print count([1,2,3,1,2,1], 1)


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


#2

your count function should also work for strings, in which case doing a division:

i/i

is a bit of a problem. Why not simply increase total by one? Since total is a simple counter to keep track of how many item occurs in sequence


#3

Thanks. I figured it out. I needed to make my variable as an integer so it can add the sum of the item. My code below seems to work now.

def count(sequence, item):
    total = 0
    for i in sequence:
        if item == i:
            i = 1
            total += i
    return total
print count([1,2,3,1,2,1], 1)

#4

just replace this with

total+=1


#5

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