List and Functions Fizz Count


#1




I cannot find a way yet to make the code read a count for the word "fizz".

I expect for it to count the word "fizz" each time, but I cannot find a way for the code to register a count for the string "fizz". What do I need to do. I have literally been stuck on this for a year.


def fizz_count(y):
    count=0
    for count in y:
         string = "fizz"
        count+=1
    return count
    
x=["fizz","buzz","fizz"]
fizz = fizz_count(x)   
print fizz


#2

okay, lets start here:

count = 0
for count in y:

using the same name for the variable you are going to store the count in, and use iterator is a bad idea. Lets change the iterator to something else like item since the iterator will hold each item of the list:

def fizz_count(y):
    count=0
    for item in y:
         print item
x=["fizz","buzz","fizz"]
fizz = fizz_count(x)   
print fizz

okay, go and run this code. As you will see, item will hold each item of the order in turn. So then we can add an if statement to check if item equals "fizz", if it does increase count and then at the end return count


#3

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