how to finish a while loop...

richie richieyan at gmail.com
Tue Feb 19 20:42:48 EST 2008


On Feb 20, 9:35 am, icarus <rsa... at gmail.com> wrote:
> > To the original poster.... what environment are you running this in?
>
>             Linux.  Xubuntu if that matters.
>
> > When I put your program in notepad and run it from the windows command
> > prompt it works.
>
>             yeah yeah...same here.
> After I got the tip that it actually worked, I went into the eclipse
> directory where the program lives, ran it there from the shell, and it
> worked.  Meaning, I didn't modify anything on the file itself (by
> accident or on purpose).
>
> > 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.
>
>           I get no 'compile' errors there.
>           I get regular execution but it just doesn't change the
> condition to False at the very end.
>           Therefore it loops forever.  I used other values like zeros
> and ones to make sure I could print the values when the interpreter
> got down to that line.  Everything checked.  Just didn't change the
> condition on the main loop.

I've changed this code a little.
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)? ")
        print answer
        #answer="no"
        if answer == "yes":
            condition=True
        elif answer == "no":
            condition=False
        print "Good bye, you don't want to continue"
        print condition
And i got this result in eclipse3.2:
Please enter an integer: 8
Please enter the second integer: 4
The result is 2
Do you want to try again (yes or no)? no
no
Good bye, you don't want to continue
True
Please enter an integer:

it seems the input "no" in eclipse's console to answer won't equal the
"no" we compare.
And when I remove the comment and I get this result:
Please enter an integer: 8
Please enter the second integer: 4
The result is 2
Do you want to try again (yes or no)? no
no
Good bye, you don't want to continue
False




More information about the Python-list mailing list