-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit_converter.py
385 lines (341 loc) · 9.61 KB
/
unit_converter.py
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
from forex_python.converter import CurrencyCodes, CurrencyRates, RatesNotAvailableError
def run():
try:
while True:
choice = int(
input(
"""Choose from the given options below-
1- Weight
2- Data
3- Length
4- Mass
5- Numeral System
6- Speed
7- Temperature
8- Currency conversion
9- Exit
: """
)
)
def http_status(choice):
match choice:
case 1:
weight()
case 2:
data()
case 3:
length()
case 4:
mass()
case 5:
numeralSystem()
case 6:
speed()
case 7:
temperature()
case 8:
currencyConversion()
case 9:
exit()
case _:
print("Invalid Input !!!")
http_status(choice)
except Exception as e:
print("Sorry, there's an unfortunate error. Try again")
run()
def weight():
try:
choose = int(
input(
"""Choose from the given list below-
1- To convert kgs to pounds
2- To convert pounds to kgs
3- Return to main menu
4- Exit - """
)
)
def http_status(c):
match c:
case 1:
KtoP()
case 2:
PtoK()
case 3:
run()
case 4:
exit()
case _:
print("Invalid input, try again. ")
weight()
http_status(choose)
except Exception as e:
print("Sorry, there's an unfortunate error. Try again")
weight()
def KtoP():
kg = float(input("KGs: "))
print(f"{kg} kgs is equal to {kg * 2.20462:.2f} pounds")
def PtoK():
pound = float(input("Pounds: "))
print(f"{pound} pounds is equal to {pound / 2.20462:.2f} kgs")
def numeralSystem():
try:
choose = int(
input(
"""Choose from the given list below-
1- To convert deca to deci
2- To convert hecto to deci
3- Return to main menu
4- Exit - """
)
)
def http_status(c):
match c:
case 1:
decaTOdeci()
case 2:
hectoTOdeci()
case 3:
run()
case 4:
exit()
case _:
print("Invalid input, try again. ")
numeralSystem()
http_status(choose)
except Exception as e:
print("Sorry, there's an unfortunate error. Try again")
numeralSystem()
def decaTOdeci():
deca = float(input("Deca: "))
print(f"{deca} deca is equal to {deca * 10:.2f} deci")
def hectoTOdeci():
hecto = float(input("Hecto: "))
print(f"{hecto} hecto is equal to {hecto * 100:.2f} deci")
def mass():
try:
choose = int(
input(
"""Choose from the given list below-
1- To convert kgs to tonnes
2- To convert grams to milligrams
3- Return to main menu
4- Exit - """
)
)
def http_status(c):
match c:
case 1:
kgTOton()
case 2:
grTOmil()
case 3:
run()
case 4:
exit()
case _:
print("Invalid input, try again.")
mass()
http_status(choose)
except Exception as e:
print("Sorry, there's an unfortunate error. Try again")
mass()
def kgTOton():
kg = float(input("KG: "))
print(f"{kg} kgs is equal to {kg * 0.001:.3f} tonnes")
def grTOmil():
gr = float(input("Grams: "))
print(f"{gr} grams is equal to {gr * 1000:.0f} milligrams")
def speed():
try:
choose = int(
input(
"""Choose from the given list below-
1- To convert kmph to mph
2- To convert mph to kmph
3- Return to main menu
4- Exit - """
)
)
def http_status(c):
match c:
case 1:
kTOm()
case 2:
mTOk()
case 3:
run()
case 4:
exit()
case _:
print("Invalid input, try again.")
speed()
http_status(choose)
except Exception as e:
print("Sorry, there's an unfortunate error. Try again")
speed()
def kTOm():
km = float(input("KM: "))
print(f"{km} km is equal to {km * 0.621371:.2f} miles")
def mTOk():
mil = float(input("Miles: "))
print(f"{mil} miles is equal to {mil * 1.60934:.2f} km")
def data():
try:
choose = int(
input(
"""Choose from the given list below-
1- To convert MBs to KBs
2- To convert TBs to GBs
3- Return to main menu
4- Exit - """
)
)
def http_status(c):
match c:
case 1:
mbTOkb()
case 2:
tbTOgb()
case 3:
run()
case 4:
exit()
case _:
print("Invalid input, try again.")
data()
http_status(choose)
except Exception as e:
print("Sorry, there's an unfortunate error. Try again")
data()
def mbTOkb():
mb = float(input("MB: "))
print(f"{mb} MBs is equal to {mb * 1000:.0f} KBs")
def tbTOgb():
tb = float(input("TB: "))
print(f"{tb} TBs is equal to {tb * 1000:.0f} GBs")
def length():
try:
choose = int(
input(
"""Choose from the given list below-
1- To convert feet to metres
2- To convert metres to inches
3- Return to main menu
4- Exit - """
)
)
def http_status(c):
match c:
case 1:
FtoM()
case 2:
MtoI()
case 3:
run()
case 4:
exit()
case _:
print("Invalid input, try again.")
length()
http_status(choose)
except Exception as e:
print("Sorry, there's an unfortunate error. Try again")
length()
def FtoM():
feet = float(input("Feet: "))
print(f"{feet} feet is equal to {feet * 0.3048:.2f} metres")
def MtoI():
metres = float(input("Metres: "))
print(f"{metres} metres is equal to {metres * 39.3701:.2f} inches")
def temperature():
try:
c = int(
input(
"""Choose from the given list below-
1- To convert celsius to fahrenheit
2- To convert fahrenheit to celsius
3- To convert celsius to kelvin
4- To convert kelvin to celsius
5- Return to the main menu
6- Exit - """
)
)
def http_status(c):
match c:
case 1:
CtoF()
case 2:
FtoC()
case 3:
CtoK()
case 4:
KtoC()
case 5:
run()
case 6:
exit()
case _:
print("Invalid input, try again.")
temperature()
http_status(c)
except Exception as e:
print("Sorry, there's an unfortunate error. Try again")
temperature()
def CtoF():
c = float(input("Celsius: "))
print(f"{c}°C is equal to {(c * 9/5) + 32:.2f}°F")
def FtoC():
f = float(input("Fahrenheit: "))
print(f"{f}°F is equal to {(f - 32) * 5/9:.2f}°C")
def CtoK():
c = float(input("Celsius: "))
print(f"{c}°C is equal to {c + 273.15:.2f}K")
def KtoC():
k = float(input("Kelvin: "))
print(f"{k}K is equal to {k - 273.15:.2f}°C")
def currencyConversion():
try:
c = int(
input(
"""Choose from the given list below to convert -
1- USD to EURO
2- EURO to USD
3- USD to INR
4- INR to USD
5- Return to main menu
6- Exit - """
)
)
def http_status(c):
match c:
case 1:
print(currencyConversionRate("USD", "EUR"))
case 2:
print(currencyConversionRate("EUR", "USD"))
case 3:
print(currencyConversionRate("USD", "INR"))
case 4:
print(currencyConversionRate("INR", "USD"))
case 5:
run()
case 6:
exit()
case _:
print("Invalid input, try again.")
currencyConversion()
http_status(c)
except Exception as e:
print("Sorry, there's an unfortunate error. Try again")
currencyConversion()
def currencyConversionRate(from_curr, to_curr):
try:
c = CurrencyRates()
conversion = c.get_rate(from_curr, to_curr)
currency_codes = CurrencyCodes()
currency_symbol = currency_codes.get_symbol(to_curr)
return f"1 {from_curr} is equal to {conversion:.2f} {currency_symbol}"
except RatesNotAvailableError:
return "Exchange rate not available for the requested currencies. "
except Exception as e:
return f"An error occured : {e}"
run()