Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44788 > unrolled thread
| Started by | Bradley Wright <bradley.wright.biz@gmail.com> |
|---|---|
| First post | 2013-05-05 17:59 -0700 |
| Last post | 2013-05-06 17:32 +1000 |
| Articles | 10 — 7 participants |
Back to article view | Back to comp.lang.python
(Learner-here) Lists + Functions = headache Bradley Wright <bradley.wright.biz@gmail.com> - 2013-05-05 17:59 -0700
Re: (Learner-here) Lists + Functions = headache alex23 <wuwei23@gmail.com> - 2013-05-05 18:21 -0700
Re: (Learner-here) Lists + Functions = headache Bradley Wright <bradley.wright.biz@gmail.com> - 2013-05-05 18:24 -0700
Re: (Learner-here) Lists + Functions = headache Bradley Wright <bradley.wright.biz@gmail.com> - 2013-05-05 18:30 -0700
Re: (Learner-here) Lists + Functions = headache Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-05-06 10:25 +0100
Re: (Learner-here) Lists + Functions = headache Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-06 01:31 +0000
Re: (Learner-here) Lists + Functions = headache Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-06 01:48 +0000
Re: (Learner-here) Lists + Functions = headache Terry Jan Reedy <tjreedy@udel.edu> - 2013-05-05 21:30 -0400
Re: (Learner-here) Lists + Functions = headache 88888 Dihedral <dihedral88888@googlemail.com> - 2013-05-05 22:39 -0700
Re: (Learner-here) Lists + Functions = headache Chris Angelico <rosuav@gmail.com> - 2013-05-06 17:32 +1000
| From | Bradley Wright <bradley.wright.biz@gmail.com> |
|---|---|
| Date | 2013-05-05 17:59 -0700 |
| Subject | (Learner-here) Lists + Functions = headache |
| Message-ID | <ed7e2a6d-ad84-49b1-afae-6d1516fc1130@googlegroups.com> |
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?
[toc] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2013-05-05 18:21 -0700 |
| Message-ID | <31f31fc5-65a8-46af-8be8-69c71901278a@tz3g2000pbb.googlegroups.com> |
| In reply to | #44788 |
On May 6, 10:59 am, Bradley Wright <bradley.wright....@gmail.com> wrote: > 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.
[toc] | [prev] | [next] | [standalone]
| From | Bradley Wright <bradley.wright.biz@gmail.com> |
|---|---|
| Date | 2013-05-05 18:24 -0700 |
| Message-ID | <bc93017c-a6bb-4e7a-bfdc-c75f52ee25d9@googlegroups.com> |
| In reply to | #44789 |
On Sunday, May 5, 2013 9:21:33 PM UTC-4, alex23 wrote: > On May 6, 10:59 am, Bradley Wright <bradley.wright....@gmail.com> > > wrote: > > > 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. Thank you Alex - much appreciated, about to implement right now!
[toc] | [prev] | [next] | [standalone]
| From | Bradley Wright <bradley.wright.biz@gmail.com> |
|---|---|
| Date | 2013-05-05 18:30 -0700 |
| Message-ID | <44cd4048-c988-4594-91d9-72ab2b56ad4c@googlegroups.com> |
| In reply to | #44790 |
On Sunday, May 5, 2013 9:24:44 PM UTC-4, Bradley Wright wrote: > On Sunday, May 5, 2013 9:21:33 PM UTC-4, alex23 wrote: > > > On May 6, 10:59 am, Bradley Wright <bradley.wright....@gmail.com> > > > > > > wrote: > > > > > > > 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. > > > > Thank you Alex - much appreciated, about to implement right now! Aha! lessons learned - got it!
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-05-06 10:25 +0100 |
| Message-ID | <mailman.1326.1367832374.3114.python-list@python.org> |
| In reply to | #44791 |
On 06/05/2013 02:30, Bradley Wright wrote: > > 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. -- If you're using GoogleCrap™ please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-05-06 01:31 +0000 |
| Message-ID | <51870803$0$29997$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #44788 |
On Sun, 05 May 2013 17:59:15 -0700, Bradley Wright wrote:
> 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.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-05-06 01:48 +0000 |
| Message-ID | <51870c02$0$29997$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #44792 |
On Mon, 06 May 2013 01:31:48 +0000, Steven D'Aprano wrote: > 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 :-( -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Terry Jan Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2013-05-05 21:30 -0400 |
| Message-ID | <mailman.1314.1367803836.3114.python-list@python.org> |
| In reply to | #44788 |
On 5/5/2013 8:59 PM, Bradley Wright wrote: > 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. -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2013-05-05 22:39 -0700 |
| Message-ID | <3a5447e4-4f20-45bb-be22-cf820f2c7737@googlegroups.com> |
| In reply to | #44788 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-05-06 17:32 +1000 |
| Message-ID | <mailman.1319.1367825546.3114.python-list@python.org> |
| In reply to | #44799 |
On Mon, May 6, 2013 at 3:39 PM, 88888 Dihedral <dihedral88888@googlemail.com> wrote: > Bradley Wright於 2013年5月6日星期一UTC+8上午8時59分15秒寫道: >> 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. Wow. You know a question's been asked frequently when even the bots can learn to answer it correctly. ChrisA
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web