Something wrong with me code:

def fizz_count(x):
count = 0
for i in x:
if i == "fizz":
count = count + 1
print count

what I am missing?


Hi @neriyah,

You need to have the function do this ...

   return count

... instead of having it display the result.

Then you can call the function, as follows ...

print fizz_count(["fizz", "buzz", "fizz", "fizz"])

With that call, the output should be 3.


whats the difference between return and print?


A print statement displays the result of evaluating an expression or a series of expressions.

A return statement can only be used inside a function. It terminates execution of the function and makes a result available to any statement that calls the function.

For example, with a return statement having been included in the fizz_count function, the following statement is able to call the function and use the result that was made available to it by that return statement ...

print fizz_count(["fizz", "buzz", "fizz", "fizz"])

You could save the result in a variable, if you would like ...

fizzes = fizz_count(["fizz", "buzz", "fizz", "fizz"])

Then you can use the saved result later on, if you want.

Having a function return a result provides much more flexibility than to have it only print it, since you can include a returned result in an expression or save it in a variable for later use.


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