Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #44791

Re: (Learner-here) Lists + Functions = headache

Newsgroups comp.lang.python
Date 2013-05-05 18:30 -0700
References <ed7e2a6d-ad84-49b1-afae-6d1516fc1130@googlegroups.com> <31f31fc5-65a8-46af-8be8-69c71901278a@tz3g2000pbb.googlegroups.com> <bc93017c-a6bb-4e7a-bfdc-c75f52ee25d9@googlegroups.com>
Message-ID <44cd4048-c988-4594-91d9-72ab2b56ad4c@googlegroups.com> (permalink)
Subject Re: (Learner-here) Lists + Functions = headache
From Bradley Wright <bradley.wright.biz@gmail.com>

Show all headers | View raw


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!

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

(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

csiph-web