-6

I want to count 'fizz' in the list and I wrote below code and it doesn't worked. Tell me why and provide a solution. I was doing this tutorial and I have to write code as their instruction but I am unable to do what they said or maybe I misunderstood them.

count = 0
def fizz_count(x):
    for string in x:
        if string == 'fizz':
            count = count + 1
        else:
            count = count
    return count

If you want to check if your answer is correct then you have to check it by interpreting it here. Note that I am not calling my own function. Instead, their is an built in feature in Codecademy tutorial it checks function by calling function with some argument.

Devesh Saini
  • 557
  • 5
  • 16
  • 2
    What exactly do you mean by "it doesn't work"? – Tim Pietzcker Jan 27 '14 at 06:31
  • What wrong with `lst.count('fizz')`? – Abhijit Jan 27 '14 at 06:31
  • @TimPietzcker I mean it is not printing the number of times the string 'fizz' is in the list x – Devesh Saini Jan 27 '14 at 06:33
  • 1
    When formatting your code, I noticed that you used tabs for indentation. Don't do this. Use four spaces for each level of indentation. (I possibly even fixed your error when I did this - mixing tabs and spaces is begging for problems in Python) – Tim Pietzcker Jan 27 '14 at 06:33
  • Well, what *is* it printing? Please also provide an example list that we can test your program on. – Tim Pietzcker Jan 27 '14 at 06:34
  • You need to declare `global count` if you want to modify `count` in an inner scope like that. Or more sanely just ditch the global and use the return value of your function. – roippi Jan 27 '14 at 06:39
  • @Abhijit I was doing the tutorial at [Codecademy](http://www.codecademy.com/courses/python-beginner-en-IZ9Ra/0/4?curriculum_id=4f89dab3d788890003000096#) and I have to write code as per their instructions but I am unable to understand what they said. – Devesh Saini Jan 27 '14 at 06:40
  • @DeveshSaini: I'm not the downvoter, but it seems you haven't provided enough detail in your question. Specifically, you don't include the part of the code where you call `fizz_count()`. – Joel Cornett Jan 27 '14 at 08:19

3 Answers3

0

You need to have the count variable inside for loop, and you dont require an else condition

def fizz_count(x):
    count=0
    for string in x:
        if string == 'fizz':
            count = count + 1
    return count

You can also write your function as

def fizz_count(x):
    return x.count('fizz')
Ashoka Lella
  • 6,631
  • 1
  • 30
  • 39
-1

Have a look at this below code. I made some correction in it. There were some syntactical error and some indentation errors which I fixed it. Have a look at it:

def fizz_count(x):
    count = 0
    for string in x:
        if string == 'fizz':
            count = count + 1
        else:
            count = count
    return count


str1 = fizz_count(['fizzers','fizzerlss','fizz','fizz'])
print str1
Furquan Khan
  • 1,586
  • 1
  • 15
  • 30
  • It is printing 2 as the occurrence of `fizz` is twice. – Furquan Khan Jan 27 '14 at 06:42
  • Your code return this error:- File "python", line 2 count = 0 ^ IndentationError: expected an indented block – Devesh Saini Jan 27 '14 at 06:45
  • Or you can simply do like this: `lst = ['fizz','fizz','fizzler'] print lst.count('fizz')` – Furquan Khan Jan 27 '14 at 06:49
  • the last str1 is just a variable used to store the count. I called the function `fizz_count` by passing the list and the returned value from that function is stored in the variable named str1. Then it is being printed. – Furquan Khan Jan 27 '14 at 06:54
  • Watch this links... Possible Duplicacy. http://stackoverflow.com/questions/16591941/python-2-7-use-a-list-as-an-argument-in-a-function?rq=1 and http://stackoverflow.com/questions/19453371/showing-and-returning-the-number-of-a-repeated-string-on-a-function?rq=1 – Furquan Khan Jan 27 '14 at 06:57
-3

Edit: Same with more explanation due to down votes

Considering your approach with a global variable count (global, because it is not within the function), you would need to do two adaptions:

  • You need to make count available locally with the keyword global
  • Since you use a global variable, you do not need to return it because it is available anyway

Code:

count = 0

def fizz_count(x):
    global count # make count available for writing in the local scope

    for string in x:
        if string == 'fizz':
            count = count + 1
    else:
        count = count

Now actually using a global variable especially for such kind of tasks is not recommended, because it takes more time to load, you have to take care of the current state and stuff like that, so you rather'd introduce a local variable and pass it via the return statement as you did.

  • Put count = 0 into function

Code

# we put count = 0 into the function

def fizz_count(x):
    count = 0 # right here

    for string in x:
        if string == 'fizz':
            count = count + 1
    else:
        count = count

    return count

Now a new count will be initialized to zero within the function.

Now there are two more things here:

  • the else statement
  • and the increment

Since the else statement is outside the loop it will be only executed if the if condition was never True during the loop. That has some use cases. But you'd put it in the loop usually. However, in your case it basically does not change anything, thats why it can be removed completely.

Well and the second point is that the increment can be written as count += 1 which is considered more readable, because it is less clumsy and the second count is merged into the +=

Therefore finally the function could be written like so

def fizz_count(x):
    count = 0 

    for string in x:
        if string == 'fizz':
            count += 1

    return count

Check also the reasonable solutions of the other answers

embert
  • 7,336
  • 10
  • 49
  • 78
  • 1
    This is not a job for `global`; `count = 0` should be inside the function. Also, the `else` clause is useless. – Tim Pietzcker Jan 27 '14 at 06:38
  • The `else` clause is also at the wrong indentation level, though it happens to not matter. – user2357112 Jan 27 '14 at 06:39
  • 1. Yes, but maybe the OP wants a global `count` 2. Useless, but it's not making the code to fail (and knowing that it can be written there is somewhat helpful sometimes) @TimPietzcker @user2357112 – embert Jan 27 '14 at 06:41