how to finish a while loop...

richie richieyan at gmail.com
Tue Feb 19 20:12:27 EST 2008


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.



More information about the Python-list mailing list