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

Alan Gauld alan.gauld at blueyonder.co.uk
Mon Jul 12 12:53:12 CEST 2004


Subject: [Tutor] Please critique my temperature_conversion.py

Looks good!

Only change I'd make is using string format operators:

if ForC == "F": t = 5/9. * (float(t) - 32) t = round(t,2) print (t0 + "F"), "is", (str(t) + "C")

   print "%.2fF is %.2fC" % (t0,t)

else: # i.e., ForC is "C" t = 9/5. * float(t) + 32 t = round(t,2) print (t0 + "C"), "is", (str(t) + "F")

   print "%.2fC is %.2fF" % (t0,t)

In fact by making the unit part of the format string you could have a single print line which might be easier to maintain should you add more conversions later (Kelvin anyone?!)... It also removes the need for the rounding operation.

if F_or_C == "F": unit = "C" else: unit = "F" print "%.2f%s is %.2f%s" % (t0, F_or_C, t, unit)

HTH,

Alan G.



More information about the Tutor mailing list