[Tutor] Please critique my temperature_conversion.py (original) (raw)
Bill Mill bill.mill at gmail.com
Mon Jul 12 01:40:25 CEST 2004
- Previous message: [Tutor] Please critique my temperature_conversion.py
- Next message: [Tutor] Please critique my temperature_conversion.py
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Sun, 11 Jul 2004 03:29:05 -0700, Dick Moores <rdm at rcblue.com> wrote:
""" # temperatureconversion.py # temperature conversion of Fahrenheit <---> Celsius
from string import upper ForC = "" while (ForC != "F" and ForC != "C"): t0 = rawinput("Enter temperature as 70F or 70 F or -12.47C: ") ForC = upper(t0[-1]) # store the F or C and ensure upper case t0 = t0[0:-1] # strip entered temp of the F or C t = t0 # get a string number to do the conversion calculation if ForC == "F": t = 5/9. * (float(t) - 32) t = round(t,2) print (t0 + "F"), "is", (str(t) + "C") else: # i.e., ForC is "C" t = 9/5. * float(t) + 32 t = round(t,2) print (t0 + "C"), "is", (str(t) + "F") """ Thanks, tutors. Dick Moores
Tutor maillist - Tutor at python.org http://mail.python.org/mailman/listinfo/tutor
thoughts: your variable names besides F_or_C are not very descriptive. Also, I would use forc or f_or_c instead of F_or_C; generally only class or function names should start with capital letters.
I would combine the last two lines of the first loop to be one line as such: t = float(t0[:-1]) note that if you leave a number out of a string slicing operation, it is assumed to be zero.
I prefer using C-style print statements, so your first print statement would look like: print "%s F is %d C" % (t, t0) I feel that this is cleaner and easier to read.
your comment after the else statement is nothing but distracting; it's unnecessary and obvious. Delete it.
Peace Bill Mill
- Previous message: [Tutor] Please critique my temperature_conversion.py
- Next message: [Tutor] Please critique my temperature_conversion.py
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]