By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,469 Members | 1,259 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,469 IT Pros & Developers. It's quick & easy.

how to finish a while loop...

P: n/a
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"
Feb 20 '08 #1
Share this Question
Share on Google+
13 Replies


P: n/a
2008/2/19, icarus <rs****@gmail.com>:
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"

--
http://mail.python.org/mailman/listinfo/python-list
That code works. Maybe you fixed it while you were mailing it =)

--
-- Guilherme H. Polo Goncalves
Feb 20 '08 #2

P: n/a
On Feb 20, 9:00 am, icarus <rsa...@gmail.comwrote:
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.
Feb 20 '08 #3

P: n/a
icarus <rs****@gmail.comwrites:
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:
>>## working on region in file /usr/tmp/python-13922e2f...
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
Feb 20 '08 #4

P: n/a
That code works. Maybe you fixed it while you were mailing it =)
>
--
-- Guilherme H. Polo Goncalves

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.
Feb 20 '08 #5

P: n/a
On Feb 19, 7:12 pm, richie <richie...@gmail.comwrote:
On Feb 20, 9:00 am, icarus <rsa...@gmail.comwrote:
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.
Feb 20 '08 #6

P: n/a
On Feb 20, 9:21 am, icarus <rsa...@gmail.comwrote:
That code works. Maybe you fixed it while you were mailing it =)
--
-- Guilherme H. Polo Goncalves

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.
Feb 20 '08 #7

P: n/a
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.

Feb 20 '08 #8

P: n/a
On Feb 20, 9:35 am, icarus <rsa...@gmail.comwrote:
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

Feb 20 '08 #9

P: n/a
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
On Feb 19, 5:35*pm, icarus <rsa...@gmail.comwrote:
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.
Feb 20 '08 #10

P: n/a
On Feb 19, 7:35 pm, icarus <rsa...@gmail.comwrote:
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'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
Feb 20 '08 #11

P: n/a
On Feb 20, 3:42 am, richie <richie...@gmail.comwrote:
On Feb 20, 9:35 am, icarus <rsa...@gmail.comwrote:
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
strip and lowercase your answer for better comparison checking.
Feb 20 '08 #12

P: n/a
"richie" schrieb
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.

I try it too in my eclipse3.2. I got the same result.
It seems very strange.
Print out "answer" and see if there is a difference ...

my $0.02
Martin
Feb 20 '08 #13

P: n/a
icarus wrote:
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
Feb 20 '08 #14

This discussion thread is closed

Replies have been disabled for this discussion.