-
Notifications
You must be signed in to change notification settings - Fork 1
/
unit_converter.py
62 lines (50 loc) · 1.21 KB
/
unit_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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
Problem
-------
**Unit Converter (temp, currency, volume, mass and more)**
Converts various units between one another. The user enters
the type of unit being entered, the type of unit they want
to convert to and then the value. The program will then
make the conversion.
Solution
--------
I'm gonna use a dictionary of lambda function to solve this
problem.
It's works only for temperature.
It's possible to add more conversions adding the functions into
the dictionary
Author
------
dbonadiman
"""
import sys
"""
>>> _temp['C_K'](10)
283.15
>>> _temp['F_C'](80)
26.666666666666668
"""
_temp = {
'C_K': lambda c: c+273.15,
'K_C': lambda k: k-273.15,
'C_F': lambda c: c*9.0/5.0+32.0,
'F_C': lambda f: (f-32.0)*5.0/9.0,
'F_K': lambda f: ((f-32.0)*5.0/9.0)+273.15,
'K_F': lambda k: ((k-273.15)*9.0/5.0)+32.0
}
def main():
try:
f = raw_input("From (C,K,F): ")
t = raw_input("To (C,K,F): ")
a = float(input("Amount: "))
except Exception:
print("You have entered a non valid input")
return 1
else:
print (_temp[f+"_"+t](a))
return 0
if __name__ == "__main__":
import doctest
doctest.testmod()
status = main()
sys.exit(status)