how to make a unit converter in python

Solutions on MaxInterview for how to make a unit converter in python by the best coders in the world

showing results for - "how to make a unit converter in python"
Lindsay
17 Jan 2017
1# Python Program for simple Unit Converter
2
3num1 = input('Enter the value: ')
4unit1 = input('Which unit do you want it converted from:  ')
5unit2 = input('Which unit do you want it converted to: ')
6
7if unit1 == "cm" and unit2 == "m":
8    ans = float(num1)/100
9    print(ans)
10elif unit1 == "mm" and unit2 == "cm":
11    ans = float(num1)/10
12    print(ans)
13elif unit1 == "m" and unit2 == "cm":
14    ans = float(num1)*100
15    print(ans)
16elif unit1 == "cm" and unit2 == "mm":
17    ans = float(num1)*10
18    print(ans)
19elif unit1 == "mm" and unit2 == "m":
20    ans = float(num1)/1000
21    print(ans)
22elif unit1 == "m" and unit2 == "mm":
23    ans = float(num1)*1000
24    print(ans)
25elif unit1 == "km" and unit2 == "m":
26    ans = float(num1)*1000
27    print(ans)
28elif unit1 == "m" and unit2 == "km":
29    ans = float(num1)/1000
30    print(ans)
31elif unit1 == "mm" and unit2 == "km":
32    ans = float(num1)/1000000
33    print(ans)
34elif unit1 == "ft" and unit2 == "cm":
35    ans = float(num1)*30.48
36    print(ans)
37elif unit1 == "ft" and unit2 == "mm":
38    ans = float(num1)*304.8
39    print(ans)
40elif unit1 == "ft" and unit2 == "inch":
41    ans = float(num1)*12
42    print(ans)
43elif unit1 == "inch" and unit2 == "cm":
44    ans = float(num1)*2.54
45elif unit1 == "inch" and unit2 == "mm":
46    ans = float(num1)*25.4
Mario
11 Jul 2018
1hiiiiiiii