Skip to content

Commit f1ba3f0

Browse files
author
Karan Goel
committed
Unit converter done
1 parent 0878c3d commit f1ba3f0

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

Numbers/unit.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
"""
2+
Unit Converter (temp, currency, volume, mass and more) - Converts
3+
various units between one another. The user enters the type of unit
4+
being entered, the type of unit they want to convert to and then
5+
the value. The program will then make the conversion.
6+
"""
7+
8+
from __future__ import division
9+
from urllib2 import urlopen
10+
import json
11+
12+
# 1 (std unit) = these many units
13+
MULTIPLIERS_TO_STD = {
14+
'length': {
15+
'cm': 0.01,
16+
'm': 1, # std unit
17+
'km': 1000,
18+
'mi': 1609.34,
19+
'ft': 0.3048
20+
},
21+
'temp': {
22+
'C': 1, # std unit
23+
'F': 33.8
24+
}
25+
}
26+
27+
# These many units = 1 (std unit)
28+
MULTIPLIERS_FROM_STD = {
29+
'length': {
30+
'cm': 100,
31+
'm': 1, # std unit
32+
'km': 0.001,
33+
'mi': 0.000621371,
34+
'ft': 3.28084
35+
},
36+
'temp': {
37+
'C': 1, # std unit
38+
'F': -17.2222
39+
}
40+
}
41+
42+
43+
def get_user_input(choice):
44+
units = ', '.join(MULTIPLIERS_TO_STD[choice].keys())
45+
source_unit = raw_input('\nEnter source unit (%s): ' % units)
46+
source_val = float(raw_input('How many %s\'s? ' % source_unit))
47+
convert_to = raw_input('Convert to? (%s): ' % units)
48+
return source_unit, source_val, convert_to
49+
50+
def get_currency(source_unit, source_val, convert_to):
51+
url = 'http://rate-exchange.appspot.com/currency?from=%s&to=%s&q=%s' % (
52+
source_unit, convert_to, str(source_val))
53+
content = urlopen(url).read()
54+
return json.loads(content)['v']
55+
56+
def main():
57+
print """Unit Converter
58+
1. Length
59+
2. Temperature
60+
3. Currency"""
61+
62+
choice = int(raw_input('What do you want to convert: '))
63+
64+
if choice == 1:
65+
source_unit, source_val, convert_to = get_user_input('length')
66+
print '%f%s = %f%s' % (source_val, source_unit,
67+
source_val * \
68+
MULTIPLIERS_TO_STD['length'][source_unit] * \
69+
MULTIPLIERS_FROM_STD['length'][convert_to], \
70+
convert_to)
71+
elif choice == 2:
72+
source_unit, source_val, convert_to = get_user_input('temp')
73+
if (source_unit, convert_to) == ('F', 'C'): # F -> C
74+
value = (source_val - 32) * (5/9)
75+
elif (source_unit, convert_to) == ('C', 'F'): # C -> F
76+
value = (source_val * (9/5)) + 32
77+
else:
78+
value = source_val
79+
print '%f%s = %f%s' % (source_val, source_unit,
80+
value, convert_to)
81+
82+
elif choice == 3:
83+
source_unit = raw_input('\nEnter source currency (eg USD, INR etc): ')
84+
source_val = float(raw_input('How many %s\'s? ' % source_unit))
85+
convert_to = raw_input('Convert to? (eg USD, INR etc): ')
86+
print '%f%s = %f%s' % (source_val, source_unit,
87+
get_currency(source_unit, source_val, convert_to),
88+
convert_to)
89+
90+
if __name__ == '__main__':
91+
main()

0 commit comments

Comments
 (0)