4/13 Lists + Functions


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

Write a function that counts how many times the string “fizz” appears in a list.

Write a function called fizz_count that takes a list x as input.
Create a variable count to hold the ongoing count. Initialize it to zero.
for each item in x:, if that item is equal to the string “fizz” then increment the count variable.
After the loop, please return the count variable.
For example, fizz_count([“fizz”,“cat”,“fizz”]) should return 2.

Returning 0 instead of 1

<What do you expect to happen instead?>

Should be returning 1 not 0. return is outside of the for loop



def fizz_count(x):# Write your function below!
  count = 0
  for item in x:
    if x == 'fizz':
      count = count + 1
  return count



#2

The same error showed up yesterday. Did you happen to copy from that post, or just make the same mistake?

x is a list, so it will never equal a string.

for item in x:
    if item == ...

#3

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