Lists + functions


#1

Here is my code... it is returning the wrong number every time.

def fizz_count(x):
count = 0
for word in x:
count = count + 1
return count

number = fizz_count(["fizz", "flop", "fizz"])

print number

any help appreciated!


Replace this line with your code.


#2

How'd you know if you're counting fizz ?
You're iterating over every element of loop, but you had to find if any item is fizz,If it is fizz then you've to increase counter by 1!

Lets see whats your code really O/P's --

def fizz_count(x): # Defined a function
    count = 0        # Created a variable to count
    for word in x:   # Looping over each item of list , word is an iteration variable
        count = count + 1  #At every iteration/looping you're incrementing count by 1
    return count # After your for loop gets finished you return the count variable

# So overall your function is counting number of elements inside it.

number = fizz_count(["fizz", "flop", "fizz"]) # It will return 3 as your function counts items of a lits

print number  # 3

So Now we know what your code does and also know what we wanted to do..
We wanted to count frequency/occurrence of "fizz".
I hope it helps! You're just one step close to the answer!
do reply if it helped you or not! :slight_smile:


#3

for word in x:
    count = count + 1

Before increasing the count, see if the word is 'fizz'

Hope that helps.


#4

Thanks so much man.. that really helped me understand and get to the bottom of my mistake!!


#6

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