Skip to content

Instantly share code, notes, and snippets.

@jasonrahm
Created September 15, 2021 19:45
Show Gist options
  • Save jasonrahm/bb5b709429326fa9150bbe2ff99eb7d6 to your computer and use it in GitHub Desktop.
Save jasonrahm/bb5b709429326fa9150bbe2ff99eb7d6 to your computer and use it in GitHub Desktop.
converting celsius, fahrenheit, and kelvin
import argparse
def c2f(val: float) -> float:
return val * 1.8 + 32
def f2c(val: float) -> float:
return (val - 32) / 1.8
def f2k(val: float) -> float:
return (val + 459.67) / 1.8
def k2f(val: float) -> float:
return val * 1.8 - 459.67
def c2k(val: float) -> float:
return val + 273.15
def k2c(val: float) -> float:
return val - 273.15
def build_parser():
parser = argparse.ArgumentParser()
parser.add_argument("scale", help='Source Scale (Celcius: c, Fahrenheit: f, Kelvin: k')
parser.add_argument("temperature", help='Known Temperature')
return parser.parse_args()
if __name__ == "__main__":
args = build_parser()
temperature = float(args.temperature)
if args.scale == 'c':
f, k = c2f(temperature), c2k(temperature)
print(f'{temperature} C = {f} F and {k} K')
elif args.scale == 'f':
c, k = f2c(temperature), f2k(temperature)
print(f'{temperature} F = {c} C and {k} K')
elif args.scale == 'k':
c, f = k2c(temperature), k2f(temperature)
print(f'{temperature} K = {c} C and {f} F')
else:
print("Scale should be c, f, or k")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment