[Tutor] Please critique my temperature_conversion.py (original) (raw)

Michael Janssen Janssen at rz.uni-frankfurt.de
Mon Jul 12 17:10:59 CEST 2004


On Mon, 12 Jul 2004, Dick Moores wrote:

Exception handling is new to me, and I haven't succeeded here yet, as you can see. One problem is that if after one successful conversion, the user next enters something that creates an exception, this repeats the previous correct result and presents the prompt again. Presenting the prompt again is fine, but I don't want the previous result repeated.

so to say, you want the user stay within the second while-loop, when she enters something "exceptional".

To archive this, you need to "continue" instead of "break": the break statements within your code breaks out of the second while-loop and go on with the main while-loop. A "continue" statement would stay within the loop but restart it from the beginn or rather start the next iteration of the loop.

Replacing break with continue perhaps doesn't solve all your problems, introduced by upgrading your user-input-while-loop with exceptions ;-)

First of all, make shure, you use an try-exception clause only when needed. In your case, only when checking the user input. Since the (very cool, by the way) while condition should be enough to check the temperature_unit, you will need one try-exception for checking the numberness of the user provided temperature.

try: float(temperature) except ValueError: print "%s is not a number" % temperature continue

will do this. (By this way, I've shown, where to put the Error-thingie :-)

Few remarks:

And how to provide a smooth exit from the program? I experimented with the exception that Ctrl+C raises, but doing this seems to make my computer go crazy. Available RAM goes to almost zero (from a usual 250-300 megabytes), and I have to reboot. (I'm using Windows XP.)

Mind sharing your code with us? Allways fun to wreak havoc ;-)

import sys sys.exit()

regards Michael



More information about the Tutor mailing list