[Tutor] Error checking - very basic question

Liam Clarke ml.cyresse at gmail.com
Sun Sep 25 13:52:01 CEST 2005


Hi Garry,

break is essential for while loops like that one. You'll see that
construct often enough in Python.

While 1 or While True followed by an if <condition> : break

Bit awkward, makes me miss the do - while construct.

continue is very useful in for loops.

Let's say I have a set of numbers, but I don't want to work with the
number 5. I have a phobia or something.

numbers = [1,2,3,4,5,6,7]

for number in numbers:
     if number == 5:
        continue
     print number

Continue will exit the loop at that point, but will continue with the
next value in numbers.
Try swapping break for continue.


On 9/25/05, Garry Rowberry <big.gaz at dsl.pipex.com> wrote:
> Thank you Liam,
>
> Works a treat, must look at the break and continue commands in more detail
>
> Regards
>
> Gaz
>
> -----Original Message-----
> From: Liam Clarke [mailto:ml.cyresse at gmail.com]
> Sent: Sunday, September 25, 2005 12:10 PM
> To: Garry Rowberry; Python Tutor Mailing List
> Subject: Re: [Tutor] Error checking - very basic question
>
> Hi Garry,
>      dau_version = None
>      while dau_version not in ("2.8", "2.1"):
>          dau_version = raw_input("\n\tIs the DAU a version 2.1 or 2.8,
> please
>                                                 enter only 2.1 or 2.8 ")
>          print"\n\t\aError! - please enter only 2.1 or 2.8."
>      else:
>          print""
>
>
> I don't know what they else is meant to do, I take it that's when your
> while loop exits.
>
> Here's how your code will run -
>
> dau_version = None
> (while statement) is dau_version "2.8" or "2.1"? Nope.
> dau_version = rawinput("Is...2.8")
> print "Error! - please enter only 2.1 or 2.8"
> is dau_version "2.8" or "2.1"? Yep - exit loop.
>
> Try doing it like this -
>
> while True:
>      dau_version = raw_input("\n\tIs the DAU a version 2.1 or 2.8, please\
>                                            enter only 2.1 or 2.8 ")
>      if dau_version in ("2.1", "2.8"):
>            break
>       print "Error! - please enter only 2.1 or 2.8"
>
>
> What will happen is that your while loop will loop forever (as True is
> always True).
> However, if dau_version is "2.1" or "2.8", the break command will be
> called, which exits out of the loop at that point.
>
> So your loop looks like this now -
>
> while True:
>    get dau_version
>    if dau_version is right, exit loop here.
>    print error message
>
> Regards,
>
> Liam Clarke
> On 9/25/05, Garry Rowberry <big.gaz at dsl.pipex.com> wrote:
> > I have a call which needs to reply 2.1 or 2.8 and report an error if not:
> >
> >
> > def ask_dau_version():
> >     """Determine the product issue of the DAU."""
> >     dau_version = None
> >     while dau_version not in ("2.8", "2.1"):
> >         dau_version = raw_input("\n\tIs the DAU a version 2.1 or 2.8,
> please
> > enter only 2.1 or 2.8 ")
> >         print"\n\t\aError! - please enter only 2.1 or 2.8."
> >     else:
> >         print""
> >     return dau_version
> >
> > I can see why it isn't working, the error message is in the wrong place,
> but
> > I can't see a simple way around it (wood for the trees)
> >
> > Gaz
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
>


More information about the Tutor mailing list