Homework 5

1. unit_change

Solution:

 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
print "please input a length(number only)"
 
length = float(raw_input('> '))
 
print "Please type a unit system (meter, mile, inch, foot, yard)"
 
unit = raw_input('> ')
 
kobe={'inch':39.370079,'foot':3.28084,'mile':0.000621,'yard':1.093613,'meter':1,
      'nm':10**(9),'um':10**(6),'mm':10**(3),'cm':10**(2),'km':10**(-3)}
 
index=[i for i, x in enumerate(kobe.keys()) if x==unit]
 
unit_values=kobe[unit]
values=[x/unit_values for x in kobe.values()]
values.remove(1)
 
del kobe[unit]
 
keys=kobe.keys()
 
output=[]
 
def unitchange(unit):  
    a1=[x*length for x in values]
    for i in range(len(keys)):
        output.append(str(a1[i])+' '+keys[i])
    return output
 
output=unitchange(unit)
print output
 

Solution:

The code document can be downloaded here.