[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

how to finish a while loop...

icarus

2/20/2008 1:01:00 AM

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"
14 Answers

Guilherme Polo

2/20/2008 1:10:00 AM

0

2008/2/19, icarus <rsarpi@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/p...
>

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

--
-- Guilherme H. Polo Goncalves

richie

2/20/2008 1:12:00 AM

0

On Feb 20, 9:00 am, icarus <rsa...@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.

Paul Rubin

2/20/2008 1:13:00 AM

0

icarus <rsarpi@gmail.com> writes:
> 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

icarus

2/20/2008 1:21:00 AM

0

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

Preston Landers

2/20/2008 1:25:00 AM

0

On Feb 19, 7:12 pm, richie <richie...@gmail.com> wrote:
> On Feb 20, 9:00 am, icarus <rsa...@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.

richie

2/20/2008 1:30:00 AM

0

On Feb 20, 9:21 am, icarus <rsa...@gmail.com> wrote:
> > 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.

icarus

2/20/2008 1:35:00 AM

0

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

richie

2/20/2008 1:43:00 AM

0

On Feb 20, 9:35 am, icarus <rsa...@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

rynt

2/20/2008 1:43:00 AM

0

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

Preston Landers

2/20/2008 1:49:00 AM

0

On Feb 19, 7:35 pm, icarus <rsa...@gmail.com> wrote:

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