AMS 209: HW 5: Problem 3ΒΆ

 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
# coding: utf8

'''
Created on November 6th, 2017

@author: Geetanjali
'''

def convert_length(length, unit):
	"""
	:param length: length to be converted to various units
	:param unit: the of unit the length entered 
	:return: returns a dictionary with various units as keys, and the value of length in that unit as values
	"""
	length_dict = {}

	if(unit == "meter"):
		length_dict["miles"] = (0.000621)*length
		length_dict["inches"] = (39.370079)*length
		length_dict["feet"] = (3.28084)*length
		length_dict["yards"] = (1.093613)*length

	elif(unit == "yard"):
		length_dict["meters"] = (0.9144)*length
		length_dict["miles"] = (0.000568182)*length
		length_dict["inches"] = (36)*length
		length_dict["feet"] = (3)*length

	elif(unit == "mile"):
		length_dict["meters"] = (1609.34)*length
		length_dict["yards"] = (1760)*length
		length_dict["inches"] = (63360)*length
		length_dict["feet"] = (5280)*length

	elif(unit == "inch"):
		length_dict["meters"] = (0.0254)*length
		length_dict["miles"] = (1.57828e-5)*length
		length_dict["yards"] = (0.0277778)*length
		length_dict["feet"] = (0.0833333)*length

	elif(unit == "foot"):
		length_dict["meters"] = (0.3048)*length
		length_dict["miles"] = (0.000189394)*length
		length_dict["inches"] = (12)*length
		length_dict["yard"] = (0.333333)*length

	return length_dict

def convert_SI(length):
	"""
	:param length: length to be converted to SI units 
	:return SI_length_dict: returns a dictionary with various SI units as keys, and the value of length in that unit as values
	"""

	SI_length_dict = {}
	
	SI_length_dict["nm"] = 10**9*length
	SI_length_dict["um"] = 10**6*length
	SI_length_dict["mm"] = 10**3*length
	SI_length_dict["cm"] = 10**2*length
	SI_length_dict["km"] = 10**(-3)*length

	return SI_length_dict

	
if __name__ == '__main__':

	length = float(input("Please input a length (number only): "))
	unit = input("Please type a unit system (meter, mile, inch, foot, yard): ")
	length_dict = convert_length(length, unit)

	#if the entered length is in meters, we directly the function convert_SI() on it. Else, we first find its value in meters and then call convert_SI()
	if(unit == "meter"):
		SI_length_dict = convert_SI(length)
	else:
		SI_length_dict = convert_SI(length_dict["meters"])

	#Printing the value of length in various units of measurement
	result = []
	for key, value in length_dict.items():
		result.append(str(value)+" "+str(key))
	print(result)

	#Printing the value of length in various SI units of measurement
	result = []
	for key, value in SI_length_dict.items():
		result.append(str(value)+" "+str(key))
	print(result)