4. List & Functions


I don't understand why I am keep getting the error:

Oops, try again. an exception was raised for fizzcount(['fizz', 'buzz']): list indices must be integers, not_

I've tried a couple of things but can't seem to get the solution. What am I doing wrong?


# Write your function below!
def fizz_count(items = ["fizz", "cat", "fizz"]):
    count = 0
    for item in items:
        if item == items["fizz"]:
            count += 1
            print count


if item == items["fizz"]

List elements in Python should be referenced by an integer, not a string.

For example, "fizz" is items[0], "cat" is items[1], and so on.

However, you can simply check if item == "fizz", as in this case there's no need to reference the list.


Thx it worked. I also needed to return the count value :slight_smile:


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