-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitConversionModule.py
More file actions
193 lines (160 loc) · 7.25 KB
/
Copy pathUnitConversionModule.py
File metadata and controls
193 lines (160 loc) · 7.25 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#Unit Conversion Module
def convert_length(value, from_unit, to_unit):
# Define conversion factors for length
length_units = {
"meters": 1,
"kilometers": 1000,
"centimeters": 0.01,
"millimeters": 0.001,
"miles": 1609.344,
"yards": 0.9144,
"feet": 0.3048,
"inches": 0.0254
}
# Convert the value to meters first
value_in_meters = value * length_units[from_unit]
# Convert the value in meters to the target unit
converted_value = value_in_meters / length_units[to_unit]
return converted_value
def convert_mass(value, from_unit, to_unit):
# Define conversion factors for mass
mass_units = {
'grams':1,
'kilograms':1000,
'milligrams':0.001,
'micrograms':0.000001,
'tonne':1000000,
'ounce':28.35,
'pound':453.6,
'US ton':9071581
}
# Convert the value to kilograms first
value_in_kilograms = value * mass_units[from_unit]
# Convert the value in kilograms to the target unit
converted_value = value_in_kilograms / mass_units[to_unit]
return converted_value
def convert_time(value, from_unit, to_unit):
# Define conversion factors for time
time_units = {
'seconds':1,
'milliseconds':0.001,
'microseconds':0.000001,
'minute':60,
'hour':3600,
'day':86400,
'week':604800,
'month':2.628E6,
'year':31536000
}
# Convert the value to seconds first
value_in_seconds = value * time_units[from_unit]
# Convert the value in seconds to the target unit
converted_value = value_in_seconds / time_units[to_unit]
return converted_value
def display_menu():
print("\nUnit Conversion Menu:")
print("1. Length")
print("2. Mass")
print("3. Time")
print("0. Exit\n")
def StartModule():
print("""
*************************************************************
* UNIT CONVERSION CALCULATOR *
*************************************************************
Welcome! Let's do some unit conversions!
How to Use the Program:
1. Enter a number wherever prompted.
2. Choose the physical quantity that you would like to convert.
3. Enter the unit of your value, then the unit you want to convert to convert to.
4. At last, enter the value itself.
--------------------------------------------------------------------------------------
| IMPORTANT: You can only convert between different units of length, mass, and time. |
--------------------------------------------------------------------------------------
""")
# Display menu
while True:
display_menu()
try:
choice = int(input("Choose the type of conversion (1-3 or 0 to exit): "))
except ValueError:
print('\nPlease enter a valid choice!')
continue
if choice == 0:
print('''
-----------------------------------------------
| Exiting the Unit Conversion module. |
-----------------------------------------------
''')
break
# Choose units for length
elif choice == 1:
print("\nLength Units:")
length_units = ["meters", "kilometers", "centimeters", "millimeters", "miles", "yards", "feet", "inches"]
for index, unit in enumerate(length_units, 1):
print(f"{index}. {unit}")
try:
from_choice = int(input("\nChoose the unit to convert from (1-8): ")) - 1
to_choice = int(input("Choose the unit to convert to (1-8): ")) - 1
except ValueError:
print('Invalid input! Please choose from any of the options')
continue
if from_choice != to_choice:
try:
value = float(input(f"Enter the value in {length_units[from_choice]}: "))
converted_value = convert_length(value, length_units[from_choice], length_units[to_choice])
print(
f'''\n{value} {length_units[from_choice]} ==> {converted_value:.10f} {length_units[to_choice]}''')
except ValueError:
print('Please enter a valid number to convert!')
continue
else:
print("You chose the same units. No conversion necessary.")
elif choice == 2:
print('\nMass Units:')
mass_units = ['grams','kilograms','milligrams','micrograms','tonne','ounce','pound','US ton']
for index, unit in enumerate(mass_units, 1):
print(f"{index}. {unit}")
try:
from_choice = int(input("\nChoose the unit to convert from (1-8): ")) - 1
to_choice = int(input("Choose the unit to convert to (1-8): ")) - 1
except ValueError:
print('Invalid input! Please choose from any of the options')
continue
if from_choice != to_choice:
try:
value = float(input(f'Enter the value in {mass_units[from_choice]}: '))
converted_value = convert_mass(value, mass_units[from_choice], mass_units[to_choice])
print(f"{value} {mass_units[from_choice]} is equal to {converted_value:.10f} {mass_units[to_choice]}")
except ValueError:
print('Please enter a valid number to convert!')
continue
else:
print("You chose the same units. No conversion necessary.")
elif choice == 3:
print('\nTime Units:')
time_units = ['seconds','milliseconds','microseconds','minute','hour','day','week','month','year']
for index, unit in enumerate(time_units, 1):
print(f'{index}. {unit}')
try:
from_choice = int(input('\nChoose the unit to convert from (1-9): ')) - 1
to_choice = int(input('Choose the unit to convert to (1-9): ')) - 1
except ValueError:
print('Invalid input! Please choose from any of the options')
continue
if from_choice != to_choice:
try:
value = float(input(f'Enter the value in {time_units[from_choice]}: '))
converted_value = convert_time(value, time_units[from_choice], time_units[to_choice])
print(f'{value} {time_units[from_choice]} is equal to {converted_value:.10f} {time_units[to_choice]}')
except ValueError:
print('Please enter a valid number to convert!')
continue
else:
print('You chose the same units. No conversion necessary.')
# You can add more conversions for mass and time similarly
else:
print("\nInvalid choice. Please choose a valid conversion type from 1-3 or 0 to exit!")
# Start the conversion program
if __name__ == '__main__':
StartModule()