[Tutor] Error checking - very basic question

Liam Clarke ml.cyresse at gmail.com
Sun Sep 25 13:09:43 CEST 2005


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