[Tutor] Please critique my temperature_conversion.py

Dick Moores rdm at rcblue.com
Mon Jul 12 15:05:26 CEST 2004


Thanks, tutors, for all the criticism! I've been up all night (Pacific 
Time) trying to incorporate all of your advice into my script. Variable 
names, print formatting, exception catching, an outer while loop, 
eliminating the need for importing from string. Learned a lot.

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. This doesn't 
happen if the user has yet to make a correct entry (such as 86F--getting 
"86F is 30C"). The exceptions I've tried to handle are: qwert, 2134qwer, 
asdf, zxc, (these 2 end in f or c), "", "     ".

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

No command line execution yet.

And Kelvin? Later!

Dick Moores

"""
# temperature_conversion.py
# temperature conversion: Fahrenheit <---> Celsius. User enters temperature
#    as a number followed by a unit of either F or C. The program uses 
the variable "number"
#   to hold this original number, and the variable "unit" to hold the 
original F or the C.
print """
       temperature_conversion.py
       This program will convert Fahrenheit to Celsius or Celsius to 
Fahrenheit.
       Enter a number followed by F or C, such as 70F, 70 f, -12.47C, etc.
       To exit program, press Ctrl+C
       """


while True:
     unit = "" # the Fahrenheit "F" or the Celsius "C"
     converted_unit = ""
     temperature = ""
     while (unit != "F" and unit != "C"):
         temperature = raw_input("Enter temperature (70F, 70 F, -12.47C, 
etc.): ")
         temperature = temperature.strip()
         try:
             unit = (temperature[-1]).upper()
         except:
             IndexError
             break
         try:
             number = float(temperature[0:-1]) # get just the number part 
of temperature
         except:
             ValueError
             break
         try:
             number_to_convert = number # and save the original number
         except:
             NameError
             break

     if unit == "F":
         try:
             converted_number = 5/9. * (float(number_to_convert) - 32)
         except:
             NameError
             break
     else:
         try:
             converted_number = 9/5. * float(number_to_convert) + 32
         except:
             NameError
             break

     if unit == "F":
         converted_unit = "C"
     else:
         converted_unit = "F"
     print '%s %.2f%s is %.2f%s' % ((" " * 44), number, unit, 
converted_number,  converted_unit)
  """



More information about the Tutor mailing list