4. lists + functions--Can you explain, pretty please?


#1

I have no trouble with the code, just I have trouble with some of the definitions. Here's the code:
def fizz_count(x):
count = 0
for item in x:
if item == "fizz":
count = count + 1
return count

print fizz_count(["fizz", "dog", "purple", "fizz", "fizz"])

Question 1: what does it mean when I initialize the the count variable by assigning it with a value zero?
Q2: then what does incrementing the total mean? i.e. count = count + 1?
Thank you in advance!


#2

Hi, @terasolver98951 ,

Because the code that you posted is not formatted, it is difficult for other users to read and debug it. After code has been pasted into the editing window for posting, you can format it by selecting it, and then by clicking the </> button above the editing area. Alternatively, you can place three backquotes on the line before the code and three backquotes on the line after the code. This will enable us to see important details, such as the indentation and underscores. If you use the backquotes, your code will be color-coded, making it especially easy to read.

Question 1: what does it mean when I initialize the the count variable by assigning it with a value zero?

Before you start counting occurrences of "fizz" within the loop, your actual count is 0, so you set count to 0 prior to the loop.

Q2: then what does incrementing the total mean? i.e. count = count + 1?

Whenever you find an occurrence of "fizz", your count needs to be increased by 1, and that statement implements that increase.


#3

@geomaticmashups,

Thank your for replying! And I will surely follow the coding instructions the next time I have a coding problem.