Skip to content

Instantly share code, notes, and snippets.

@halfnibble
Created May 6, 2015 19:31
Show Gist options
  • Save halfnibble/18103af180be5c1deeeb to your computer and use it in GitHub Desktop.
Save halfnibble/18103af180be5c1deeeb to your computer and use it in GitHub Desktop.
Reusable functions
# Reusable temperature conversion function
def convert_temperature(to_scale, temperature):
if to_scale == "f" or to_scale == "F":
# Convert to Fahrenheit
return temperature * 9.0 / 5 + 32
elif to_scale == "c" or to_scale == "C":
# Convert to Celsius
return (temperature - 32) * 5.0 / 9.0
else:
print "Invalid to_scale argument."
# Examples
print convert_temperature("f", 0.0)
# prints 32.0
print convert_temperature("c", 212.0)
# prints 100.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment