Some of what I am going to write may be a little too much for a 9 year old, but if he was able to produce such neat, structured code on his own, he should be ready for some further challenge.
So to make things less repetitive, all you have to do is figure out how to make what appears different to share a common framework. For the temperature conversions, they are all of the form:
return_value = (input_value + a) * b + c
Lazy programmers are better programmers, so you don´t want to figure out all six possible transformations by hand, so note that the reverse transformation has the form:
input_value = (return_value - c) / b - a
Furthermore, even specifying three of six transformations is too many, since you can always convert, e.g. from Celsius to Kelvin and then to Fahrenheit. So we just need to choose a BASE_UNIT
and encode a transformation to or from it to all other. This will also make it a breeze to include Rankine degrees or any other of the weird scales if you would so desire in the future.
So the code below is probably going to end up being longer than what you posted, but the main program is just four lines of code, there's quite a lot of error checking, and it would be a breeze to update to new units:
UNITS = 'fck'
BASE_UNIT = 'k'
UNIT_NAMES = {'f' : 'Fahrenheit', 'c' : 'Celsius', 'k' : 'Kelvin'}
CONVERSION_FACTORS = {'fk' : (-32., 5. / 9., 273.16),
'ck' : (273.16, 1., 0.)}
def get_temperature():
print "What is your temperature?"
while True:
temperature = raw_input()
try:
return float(temperature)
except ValueError:
print "Not a valid temperature. Could you please do this again?"
def convert_temp(temp, unit_from, unit_to) :
if unit_from != BASE_UNIT and unit_to != BASE_UNIT :
return convert_temp(convert_temp(temp, unit_from, BASE_UNIT),
BASE_UNIT, unit_to)
else :
if unit_from + unit_to in CONVERSION_FACTORS :
a, b, c = CONVERSION_FACTORS[unit_from + unit_to]
return (temp + a) * b + c
elif unit_to + unit_from in CONVERSION_FACTORS :
a, b, c = CONVERSION_FACTORS[unit_to + unit_from]
return (temp - c) / b - a
else :
msg = 'Unknown conversion key \'{0}\''.format(unit_from + unit_to)
raise KeyError(msg)
def pretty_print_temp(temp, unit_from) :
if unit_from not in UNITS :
msg = 'Unknown unit key \'{0}\''.format(unit_from)
raise KeyError(msg)
txt = 'This is what it would be if it was {0}'
print txt.format(UNIT_NAMES[unit_from])
for unit_to in UNITS.replace(unit_from, '') :
print '{0:.1f} degrees {1}'.format(convert_temp(temp, unit_from,
unit_to),
UNIT_NAMES[unit_to])
if __name__ == '__main__' :
print "This program tells you the different temperatures."
temperature = get_temperature()
for unit_from in UNITS :
pretty_print_temp(temperature, unit_from)
EDIT As an aside, for minimal length without modifying the logic of the original code, the following would work:
print "This program tells you the different temperatures."
temperature = get_temperature()
text = ('This is what it would be if it was {0}:\n'
'{1:.1f} {2}\n{3:.1f} {4}')
print
Celsius = (temperature - 32) / 1.8
Kelvin = Celsius + 273.15
print text.format('Fahrenheit', Celsius, 'degrees Celsius', Kelvin, 'Kelvin')
Fahrenheit = temperature * 9 / 5 + 32
Kelvin = temperature + 273.15
print text.format('Celsius', Fahrenheit, 'degrees Fahrenheit', Kelvin,
'Kelvin')
Celsius = temperature - 273.15
Fahrenheit = Celsius * 9 / 5 + 32
print text.format('Kelvin', Celsius, 'degrees Celsius', Fahrenheit,
'Fahrenheit')
Subscriber certifies to Stack Exchange that if Subscriber is an individual (i.e., not a corporate entity), Subscriber is at least 13 years of age
\$\endgroup\$