Function returning wrong output


#1

So I'm on the lesson where you have to determine the amount of soda in a list or something I've done the assignment, but, well, it's not working. My code is:


def fizz_count(x):
    count = 0
    for soda in x:
        if x == "fizz":
            count = count + x
    return count

Although the function is supposed to return 1, for some reason it returns 0. What's wrong?


#2

here:

if x == "fizz":

x is a list, "fizz" is a string, this condition will never evaluate to true

we build a loop so we can compare each item in list with "fizz"

Furthermore:

count = count + x

count is a integer, x is a list, how does list + integer work? Why not simply increase the count by one when you find a hit?


#3

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