[Tutor] Please critique my temperature_conversion.py

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

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

Few remarks:

* float(temperature) needn't be assigned to a variable. You
can in order to reuse it later (and do the float-conversion only once),
but you don't need to. Nevertheless ValueError is raised, when temperature
is an improper value for float.

* You need to mentally keep track of which variables get assigned during
the second while loop: say you need "unit" and "temperatue" for later
processing, the second while-loop *must not* end unless *both* of these
are defined. Otherwise it may happens what you've experienced with old
values getting "recycled". Using "continue" instead of break is one key to
make shure the while-loop doesn't stops before *something* is done.
Additionally you have to figure out a way to process unit and temperature,
that assures the while-loop is only ended when both (or how many you need)
variables are "freshly" asigned.

* Don't use except NameError clauses that much. A missing variable is
often a sever programm-logic error and should not occur in the final
programm. With other words: improve your second while-loop so that it will
either "continue" or successfully assign (and test) all needed variables.
The follwing code (where you actually do the conversions) can then assume
that everything is there and needn't catch NameError's.

* On Exceptions print a message, otherwise you might misunderstand what
happens within and between your while-loops. And a innocent user might
never figure out what to do to make it run.

* Try to keep the code-lines short (<80chars). Helps reading & testing
your code...

> 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


More information about the Tutor mailing list