440,469 Members | 1,259 Online
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
13 Replies

 P: n/a 2008/2/19, 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" -- 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

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

 P: n/a On Feb 20, 9:21 am, icarus

 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

 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

 P: n/a On Feb 19, 7:35 pm, icarus

 P: n/a On Feb 20, 3:42 am, richie

 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.