how to finish a while loop...

I

icarus

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"
 
G

Guilherme Polo

2008/2/19 said:
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"

That code works. Maybe you fixed it while you were mailing it =)
 
R

richie

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.
 
P

Paul Rubin

icarus said:
For some reason, it behaves as infinite loop although I changed its
condition. Please tell me what I'm doing wrong. Thanks in advance.

It worked when I tried it:
Please enter an integer: 8
Please enter the second integer: 3
The result is 2
Do you want to try again (yes or no)? yes
Please enter an integer: 8
Please enter the second integer: 2
The result is 4
Do you want to try again (yes or no)? no
Good bye, you don't want to continue
 
I

icarus

That code works. Maybe you fixed it while you were mailing it =)

This is weird mate.
I'm using eclipse 3.2 with the pydev plugin. There it loops forever -
from the eclipse console.
Two hours of trying, changing the code...finally gave up.

Then I got your reply. Opened up a regular console and executed it
from there.
And voila....it works! Well, after this I'm going back to the old
trusty shell.

Thanks again mate.
 
P

Preston Landers

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.
 
R

richie

This is weird mate.
I'm using eclipse 3.2 with the pydev plugin. There it loops forever -
from the eclipse console.
Two hours of trying, changing the code...finally gave up.

Then I got your reply. Opened up a regular console and executed it
from there.
And voila....it works! Well, after this I'm going back to the old
trusty shell.

Thanks again mate.

I try it too in my eclipse3.2. I got the same result.
It seems very strange.
 
I

icarus

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.
 
R

richie

Linux. Xubuntu if that matters.


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).


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
 
R

rynt

Wouldn't hurt to send an email or message, along with the code, to
Fabio Zdronzy, the maintainer of PyDev explaining what happened.
He'll probably see this thread, but then again, maybe not.

Ruben
 
P

Preston Landers

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'm pretty sure this is an eclipse console issue. Since you have it,
try stepping through the program in the debugger and inspect the
answer variable after raw_input returns. On my setup, at that point
answer is 'no\r', but raw_input() is supposed to strip line endings.
I'm guessing the console supplied 'no\r\n' and only the \n was
stripped. Could be that python does not recognize that eclipse's
console uses CRLF line endings.

As a workaround you can do raw_input().strip().

You might check the eclipse issue tracker to see if this a known
issue.

Preston
 
C

Chris

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

strip and lowercase your answer for better comparison checking.
 
B

Bruno Desthuilliers

icarus a écrit :
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.

Problem mostly solved, so I'll just comment a bit on the code:
condition = True

while ( condition ):

You don't need the parens here

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 ):

You don't need (nor want) the parens here:

except ZeroDivisionError:
print "\nDivision by zero detected"
except( ValueError ):

idem.

Also, the user will have to start over (ie: re enter 2 integers) if it
fails the second time.
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

This line is basically a noop. You don't need this part of the branch.
elif answer == 'no':
condition = False

This could be rewritten as:

if answer == 'no':
condition = False

which is a very verbose way to say:

condition = (answer != 'no')


print "Good bye, you don't want to continue"


here's a possible rewrite:

class DecodeError(RuntimeError):
pass

def nodecode(val):
return val

def decodeint(val):
try:
return int(val)
except ValueError:
raise DecodeError("%s is not an integer" % val)

YESNO={'yes':True,'y':True,'no':False,'n':False}
def decodeyesno(val):
try:
return YESNO[val.strip().lower()]
except KeyError:
raise DecodeError("please answer 'yes' or 'no'")

def read_input(prompt, decode=nodecode):
while True:
val = raw_input(prompt)
try:
return decode(val)
except DecodeError, e:
print e

def main():
run = True
while run:
integer_one = read_input("Please enter an integer: ", decodeint)
integer_two = read_input(
"Please enter the second integer: ",
decodeint
)
try:
division = integer_one / integer_two
except ZeroDivisionError:
print "Division by zero detected"
else:
print "The result is", division
run = read_input(
"Do you want to try again (yes or no)? ",
decodeyesno
)

print "Good bye, you don't want to continue"

if __name__ == '__main__':
main()
 
J

Jeff Schwab

icarus said:
Opened up a regular console and executed it
from there.
And voila....it works! Well, after this I'm going back to the old
trusty shell.

+1 QOTW
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top