-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.py
31 lines (20 loc) · 1.03 KB
/
converter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def pounds_to_kilograms(pound):
return float(pound) / 2.205
def kilograms_to_pounds(kg):
return float(kg) * 2.205
def generic_conversion(form, units, forward_fn, backward_fn):
measure = input("What is the " + form + " to be converted? ")
unit = input("Is that in " + units[0][0] + " or " + units[1][0] + " (" + units[0][1] + " / " + units[1][1] + ")? ")
if unit not in [units[0][1], units[1][1]]:
print("I'm sorry I don't understand that choice, exiting.")
else:
if unit == units[0][1]:
print("That is", round(forward_fn(measure), 2), units[1][0] + ".")
else:
print("That is", round(backward_fn(measure), 2), units[0][0] + ".")
def convert():
generic_conversion("temperature", [["Celsius", "c"], ["Fahrenheit", "f"]],
lambda x: float(x) * 9 / 5 + 32,
lambda x: (float(x) - 32) * 5 / 9)
generic_conversion("weight", [["Pounds", "p"], ["Kilograms", "k"]],
pounds_to_kilograms, kilograms_to_pounds)