Glitch in "A Day at the supermarket"


#1

Tried running this code below and it keeps saying the answer should be 1 when clearly it should be 3. I am unable to proceed with the lessons can someone please assist?

def fizz_count(x):
    count = 0
    for number in stuff:
        if number == "fizz":
            count = count + 1 
            print number
            print count
    return count
        
stuff = ['fizz', 'fizz', 'fizz', 'buzz', 'buzz', 'buzz']
fizz = fizz_count(stuff)
print "this is outside the loop"
print fizz

#2

you loop over stuff, why? This means that if we add a second function call:

print fizz_count(['fizz','buzz', 'test'])

it also returns 3 for this function call, even though the list only contains one fizz. That can't be right

the great thing about function is that they can do a repetitive task like counting fizzes in a a list

or you import the function into another python script (great for when you code grows), but if we would attempt to import your function:

from your_script import fizz_count

we will get an error saying stuff is undefined

make sure your function works for any argument passed into the function at function call


#3

Thanks I'm looping through stuff as it contains all of the list items. Thought that is what was asked.

Any further suggestions?


#4

but this would mean your function only works for a single list. To be honest, that just sucks, look:

def fizz_count(x):
    count = 0
    for number in stuff:
        if number == "fizz":
            count = count + 1 
            print number
            print count
    return count
        
stuff = ['fizz', 'fizz', 'fizz', 'buzz', 'buzz', 'buzz']
fizz = fizz_count(stuff)
print "this is outside the loop"
print fizz
print fizz_count(['fizz','buzz', 'test'])

i added a function call, which outputs 3, but the list only contains 1 fizz?

when calling a function, we supply an argument, what happens to this argument?


#5

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