(Learner-here) Lists + Functions = headache

B

Bradley Wright

Hey guys and gals doing this tutorial(codecademy) and needed a bit help from the experienced.

I'm writing a function that takes a list(one they supply during runtime)
here's what my function is supposed to do

1. for each instance of the string "fizz" make a count
2. Finally return that count

here's my code:

def fizz_cout(x):
count = 0
for item in x:
while item == "fizz":
count += 1
return count

Please remember that i am a eager beginner, where am i going wrong?
 
A

alex23

def fizz_cout(x):
    count = 0
    for item in x:
        while item == "fizz":
            count += 1
            return count

Please remember that i am a eager beginner, where am i going wrong?

There are several problems with your code:
    for item in x:
        while item == "fizz":
            count += 1

The `for` takes an item out of the list `x`. If that item is the
string 'fizz', it increments count. As it's a `while` loop, it will
continue to increment for as long as `item` is 'fizz'. Since the while
loop doesn't look up another list item, it will remain as 'fizz' until
the end of time. Well, it would except for your second bug:
        while item == "fizz":
            count += 1
            return count

The very first time it encounters a list item that is 'fizz', it adds
one to `count`, then exits the function passing back `count`.

You want to move the return to _outside_ the for loop, and you want to
change your `while` condition to an `if` instead.
 
B

Bradley Wright

There are several problems with your code:








The `for` takes an item out of the list `x`. If that item is the

string 'fizz', it increments count. As it's a `while` loop, it will

continue to increment for as long as `item` is 'fizz'. Since the while

loop doesn't look up another list item, it will remain as 'fizz' until

the end of time. Well, it would except for your second bug:








The very first time it encounters a list item that is 'fizz', it adds

one to `count`, then exits the function passing back `count`.



You want to move the return to _outside_ the for loop, and you want to

change your `while` condition to an `if` instead.

Thank you Alex - much appreciated, about to implement right now!
 
T

Terry Jan Reedy

Hey guys and gals doing this tutorial(codecademy) and needed a bit help from the experienced.

I'm writing a function that takes a list(one they supply during runtime)
here's what my function is supposed to do

Do they supply an example so you can test both your comprehension and
code? I think most specs given in natural language need such.
1. for each instance of the string "fizz" make a count
2. Finally return that count

Did you create an example problem to test your code? If not, do so.
Did you run your function with example data? If not, do so.
here's my code:

def fizz_cout(x):
count = 0
for item in x:
while item == "fizz":
count += 1
return count

Here is hint as to some of what needs to be improved: this function
will return either 1 or None. You should have discovered that by testings.
 
S

Steven D'Aprano

Hey guys and gals doing this tutorial(codecademy) and needed a bit help
from the experienced.

I'm writing a function that takes a list(one they supply during runtime)
here's what my function is supposed to do

1. for each instance of the string "fizz" make a count
2. Finally return that count

here's my code:

def fizz_cout(x):
count = 0
for item in x:
while item == "fizz":
count += 1
return count

Please remember that i am a eager beginner, where am i going wrong?

Lots of places, sorry. The first thing you're doing is hoping that we
will guess what error you are getting. In this case, it so happens that
we can, but that will not always be the case. You should always give us
the code (which you have done), the data it is running on, the expected
result, and the actual result. Within reason: don't bombard us with
10,000 lines of code and 3MB of data.

Now, moving on to your function: try walking through it yourself in your
head. You start off by defining a value, then start iterating over each
value in x, one at a time.


count = 0
for item in x:

Suppose x = ["buzz", "fizz", "buzz", "fizz"]. Then you should get a
result of 2. So far, you start with count = 0. Then you enter the for
loop, and item gets the value "buzz". The next line enters a while loop:

while item == "fizz":

Since item does *not* equal "fizz", the body of the while loop does not
run at all, and Python jumps past the while loop, which takes it to the
end of the for loop. So Python goes on to the next item. This time item
gets set to "fizz". So you enter the while loop again, only this time
item *does* equal "fizz":

while item == "fizz":
count += 1
return count


Oh-oh, trouble ahead. But luckily, you have two bugs, and they *almost*
cancel themselves out. The first problem is that the while loop would be
an infinite loop, going around and around and around over and over again,
since you enter it with item == "fizz" but item always stays equal to
"fizz". So the first two lines would keep adding one to count, over and
over again, until count is so big your computer runs out of memory (and
that might take *months*).

Fortunately, the very next line *almost* overcomes that bug. It doesn't
*fix* it, but it does reduce the severity. After adding one to count the
very first time, Python hits the line "return count", which immediately
exits the function, jumping out of the (infinite) while loop and the for-
loop. So your function always returns either 0 (if there are no "fizz" in
the list at all) or 1 (if there is any "fizz").

So, you have two problems, and they both need to be fixed:

1) The "return count" line must not happen until the for-loop has
completed looking at each item in the list. So it must be outside the for-
loop, not inside it. Remember that Python decides what is inside the loop
by its indentation.

2) You don't want an infinite loop inside the for-loop. There is no need
to have two loops at all. The outer for-loop is sufficient. You look at
each item *once*, not over and over again, and decide *once* if you
should add one to count, then go on to the next item.
 
S

Steven D'Aprano

So your function always returns either 0 (if there are no
"fizz" in the list at all) or 1 (if there is any "fizz").

Correction: (thanks to Terry for pointing this out). It will return None
or 1, not 0.

How easy it is to fall into the trap of assuming the function will do
what you intend it to do, instead of what you actually tell it to do :-(
 
8

88888 Dihedral

Bradley Wrightæ–¼ 2013å¹´5月6日星期一UTC+8上åˆ8時59分15秒寫é“:
Hey guys and gals doing this tutorial(codecademy) and needed a bit help from the experienced.



I'm writing a function that takes a list(one they supply during runtime)

here's what my function is supposed to do



1. for each instance of the string "fizz" make a count

2. Finally return that count



here's my code:



def fizz_cout(x):

count = 0

for item in x:

while item == "fizz":

count += 1
return count
This is not indented right in the scope to return
the total count.
 
C

Chris Angelico

Bradley Wright©ó 2013¦~5¤ë6¤é¬P´Á¤@UTC+8¤W¤È8®É59¤À15¬í¼g¹D¡G

This is not indented right in the scope to return
the total count.

Wow. You know a question's been asked frequently when even the bots
can learn to answer it correctly.

ChrisA
 
M

Mark Lawrence

Aha! lessons learned - got it!

The next lesson is to read the link given in my signature, digest it and
take action to avoid masses of superfluous newlines in your responses.

TIA.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top