-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweight_converter_V2.py
More file actions
104 lines (79 loc) · 2.73 KB
/
weight_converter_V2.py
File metadata and controls
104 lines (79 loc) · 2.73 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
from colorama import Fore,Style,init
init(autoreset=True)
#valid inputs (For now)
# will try to add more units in future
supported_weights={
"kg": "kilogram",
"g" : "gram",
"lb": "pounds",
"t" : "metric tonne",
"mg": "milligram"
}
# conversion of units in base unit (kilograms)
to_kg={
"kg": lambda x:x,
"g" : lambda x:x/1000,
"lb": lambda x:x*0.453592,
"t" : lambda x:x*1000,
"mg" : lambda x:x/1000000
}
# conversion from base unit to target unit
Target={
"kg": lambda x:x,
"g" : lambda x:x*1000,
"lb": lambda x:x/0.453592,
"t" : lambda x:x/1000,
"mg": lambda x:x*1000000
}
#main menu
# info about the converter ,styling etc...
def menu():
print(Fore.LIGHTMAGENTA_EX+'''
█░█░█ █▀▀ █ █▀▀ █░█ ▀█▀ █▀▀ █▀█ █▄░█ █░█ █▀▀ █▀█ ▀█▀ █▀▀ █▀█
▀▄▀▄▀ ██▄ █ █▄█ █▀█ ░█░ █▄▄ █▄█ █░▀█ ▀▄▀ ██▄ █▀▄ ░█░ ██▄ █▀▄''')
print(Fore.CYAN+"="*63)
print(Fore.BLUE+"\nValid Weight Units:")
print(Fore.CYAN+"-"*20)
# used above dictionaries for keys and values
for unit,unit_name in supported_weights.items():
print(Fore.WHITE+f"{unit} = {unit_name}")
print(Fore.WHITE+"-"*20)
#user input: enter the weight you want to convert
def ent_weight(prompt):
while True:
try:
return float(input(Fore.YELLOW+prompt))
except ValueError:
print(Fore.RED+"Invalid number!")
except Exception:
print(Fore.RED+"Error! Unexpected")
#user input: Enter the hte unit of the provided weight
def ent_unit(prompt):
while True:
try:
unit=input(Fore.YELLOW+prompt).strip().lower()
if unit in supported_weights:
return unit
print(Fore.RED+f"Invalid unit! Use: {','.join(supported_weights.keys())}")
except Exception:
print(Fore.RED+"Error! Unexpected")
# conversion process:
def convert(weight,unit,target):
base=to_kg[unit](weight)
return Target[target](base)
#=========MAIN LOOP=================
while True:
menu()
weight=ent_weight("Enter the weight:")
unit=ent_unit("Enter the source unit (kg/g/lb/t/mg):")
target=ent_unit("Enter the target unit (kg/g/lb/t/mg):")
result=convert(weight,unit,target)
print(Fore.GREEN+f"Result:{result:.2f}{target}")
choice=input(Fore.YELLOW+"\nConvert again? (y/n/c(clear history))?").lower()
if choice=="y":
continue
elif choice=="c":
print("\033c",end="")
else:
print("Goodbye!👋")
break