how to finish a while loop...

Preston Landers planders at gmail.com
Tue Feb 19 20:24:32 EST 2008


On Feb 19, 7:12 pm, richie <richie... at gmail.com> wrote:
> On Feb 20, 9:00 am, icarus <rsa... at gmail.com> wrote:
>
>
>
> > Hi all, i'm new to python.  Learning on my own how to ask a user to
> > finish a loop or not.
> > For some reason, it behaves as infinite loop although I changed its
> > condition.  Please tell me what I'm doing wrong. Thanks in advance.
>
> > condition = True
>
> > while ( condition ):
>
> >         try:
> >                 integer_one = int ( raw_input( "Please enter an
> > integer: " ) )
> >                 integer_two = int ( raw_input( "Please enter the
> > second integer: " ) )
> >                 division = integer_one / integer_two
>
> >         except( ZeroDivisionError ):
> >                 print "\nDivision by zero detected"
> >         except( ValueError ):
> >                 print "\nYou didn't enter an integer"
> >         else:
> >                 print "The result is", division
> >                 answer = raw_input("Do you want to try again (yes or
> > no)? ")
> >                 if answer == 'yes':
> >                         condition
> >                 elif answer == 'no':
> >                         condition = False
>
> > print "Good bye, you don't want to continue"
>
> condition = True
>
> while ( condition ):
>     try:
>         integer_one = int ( raw_input( "Please enter an integer: " ) )
>         integer_two = int ( raw_input( "Please enter the second
> integer: " ) )
>         division = integer_one / integer_two
>     except( ZeroDivisionError ):
>         print "\nDivision by zero detected"
>     except( ValueError ):
>         print "\nYou didn't enter an integer"
>     else:
>         print "The result is", division
>         answer = raw_input("Do you want to try again (yes or no)? ")
>     if answer == 'yes':
>         condition
>     elif answer == 'no':
>         condition = False
>         print "Good bye, you don't want to continue"
> Try this.
> The indent is very important in python. Take more care about it.
> You'll find python is very good for us.


Wouldn't your version get a NameError on the "if answer == 'yes'" line
in the case of a ZeroDivisionError or ValueError?

To the original poster.... what environment are you running this in?
When I put your program in notepad and run it from the windows command
prompt it works.  But when I paste it into eclipse and run it
eclipse's console, it doesn't work because answer seems to have a
stray '\r' carriage return (CR) and therefore the comparison to 'no'
fails.



More information about the Python-list mailing list