4

So I'm trying to learn Python using codecademy but I'm stuck. It's asking me to define a function that takes a list as an argument. This is the code I have:

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

It's probably something stupid I've done wrong, but it keeps telling me to make sure the function only takes one parameter, "x". def fizz_count(x): doesn't work either though. What am I supposed to do here?

Edit: Thanks for the help everyone, I see what I was doing wrong now.

Taint
  • 67
  • 1
  • 1
  • 10
  • Possible duplicate of http://stackoverflow.com/questions/2322068/python-passing-list-as-argument – devnull May 16 '13 at 15:48
  • @devnull That question is about mutation, this is about not unpacking `*args`. – askewchan May 16 '13 at 19:26
  • @askewchan Right. Similar to http://stackoverflow.com/questions/817087/call-a-function-with-argument-list-in-python, though. – devnull May 17 '13 at 04:48

6 Answers6

3

There are a handful of problems here:

  1. You're trying to iterate over fizz_count. But fizz_count is your function. x is your passed-in argument. So it should be for x in x: (but see #3).
  2. You're accepting one argument with *x. The * causes x to be a tuple of all arguments. If you only pass one, a list, then the list is x[0] and items of the list are x[0][0], x[0][1] and so on. Easier to just accept x.
  3. You're using your argument, x, as the placeholder for items in your list when you iterate over it, which means after the loop, x no longer refers to the passed-in list, but to the last item of it. This would actually work in this case because you don't use x afterward, but for clarity it's better to use a different variable name.
  4. Some of your variable names could be more descriptive.

Putting these together we get something like this:

def fizz_count(sequence):
    count = 0
    for item in sequence:
        if item == "fizz":
           count += 1
    return count

I assume you're taking the long way 'round for learning porpoises, which don't swim so fast. A better way to write this might be:

def fizz_count(sequence):
    return sum(item == "fizz" for item in sequence)

But in fact list has a count() method, as does tuple, so if you know for sure that your argument is a list or tuple (and not some other kind of sequence), you can just do:

def fizz_count(sequence):
    return sequence.count("fizz")

In fact, that's so simple, you hardly need to write a function for it!

kindall
  • 178,883
  • 35
  • 278
  • 309
  • Thank you for taking the time to write out such a helpful response. Yeah codecademy has a habit of making you do everything in the longest and most inefficient way possible, but at least I'm still learning! – Taint May 16 '13 at 16:34
1

when you pass *x to a function, then x is a list. Do either

def function(x):
   # x is a variable
   ...
function('foo') # pass a single variable
funciton(['foo', 'bar']) # pass a list, explicitly

or

def function(*args):
   # args is a list of unspecified size
   ...
function('foo') # x is list of 1 element
function('foo', 'bar') # x is list with two elements
Jakub M.
  • 32,471
  • 48
  • 110
  • 179
1

Your function isn't taking a list as an argument. *x expands to consume your passed arguments, so your function is expecting to be called like this:

f(1, 2, 3)

Not like this:

f([1, 2, 3])

Notice the lack of a list object in your first example. Get rid of the *, as you don't need it:

# Write your function below!    
def fizz_count(lst):
    count = 0

    for elem in lst:
        if elem == "fizz":
            count += 1

    return count

You can also just use list.count:

# Write your function below!    
def fizz_count(lst):
    return lst.count('fizz')
Blender
  • 289,723
  • 53
  • 439
  • 496
0

Try this:

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

Sample :

>>> fizz_count(['test','fizz','buzz'])
1

for i in x: will iterate through every elements of list x. Suggest you to read more here.

Thanakron Tandavas
  • 5,615
  • 5
  • 28
  • 41
0

It must be a typo. You're trying to iterate over the function name.

John Smith Optional
  • 22,259
  • 12
  • 43
  • 61
0

try this:

def fizz_count(x):
    counter = 0
    for element in x:
        if element == "fizz":
            counter += 1
    return counter
Christian Geier
  • 2,059
  • 2
  • 21
  • 27