I am trying to figure out this problem on Codecademy, but I can not figure it out. Ok so I need to make this so it will return the number of times a word is said in a list.
Here is what it says to do:
Write a function that counts how many times the string "fizz" appears in a list.
1.Write a function called fizz_count that takes a list x as input.
2.Create a variable count to hold the ongoing count. Initialize it to zero.
3.for each item in x:, if that item is equal to the string "fizz" then increment the count variable.
4.After the loop, please return the count variable.
For example, fizz_count(["fizz","cat","fizz"]) should return 2.
Then here is what I have written:
def fizz_count(x):
count = 0
for item in x:
if item == "fizz":
count = count + 1
return count
To look at this lesson the number is A Day at the Supermarket 4/13
.count()on lists, so your function could return justx.count('fizz'). Also, instead ofcount = count + 1you can usecount += 1. This is shorted and avoids doubling the code for the lvalue (count) in the idiom.