-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemprature_converter_V2.py
More file actions
92 lines (66 loc) · 2.42 KB
/
Copy pathTemprature_converter_V2.py
File metadata and controls
92 lines (66 loc) · 2.42 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
from colorama import Fore,Style,init
init(autoreset=True)
#valid inputs (For now)
supported_units={
"C": "Celsius",
"K": "Kelvin",
"F": "Fahrenheit",
"R": "Rankine"
}
# conversion of units in base unit (Celsius)
to_celsius={
"C": lambda x:x,
"K": lambda x:x-273.15,
"F": lambda x:(x-32)*5/9,
"R": lambda x:(x-491.67)*5/9
}
# conversion from base unit to target unit
Target={
"C": lambda x: x,
"K": lambda x: x + 273.15,
"F": lambda x: (x * 9 / 5) + 32,
"R": lambda x: (x + 273.15) * 9 / 5
}
def menu():
print(Fore.LIGHTMAGENTA_EX+'''
▀█▀ █▀▀ █▀▄▀█ █▀█ █▀▀ █▀█ ▄▀█ ▀█▀ █░█ █▀█ █▀▀ █▀▀ █▀█ █▄░█ █░█ █▀▀ █▀█ ▀█▀ █▀▀ █▀█
░█░ ██▄ █░▀░█ █▀▀ ██▄ █▀▄ █▀█ ░█░ █▄█ █▀▄ ██▄ █▄▄ █▄█ █░▀█ ▀▄▀ ██▄ █▀▄ ░█░ ██▄ █▀▄''')
print(Fore.CYAN+"="*85)
print(Fore.BLUE+"\nValid Temperature Units:")
print(Fore.CYAN+"-"*35)
for unit,unit_name in supported_units.items():
print(Fore.WHITE+f"{unit} = {unit_name}")
print(Fore.WHITE + "-" * 35)
def ent_temp(prompt):
while True:
try:
return float(input(Fore.YELLOW+prompt))
except ValueError:
print(Fore.RED+"Invalid number!")
except Exception:
print(Fore.RED+"Error! Unexpected")
def ent_unit(prompt):
while True:
unit=input(Fore.YELLOW+prompt).strip().upper()
if unit in supported_units:
return unit
print(Fore.RED+f"Invalid unit! use: {','.join(supported_units.keys())}")
def convert(temp,unit,target):
base=to_celsius[unit](temp)
return Target[target](base)
#=========MAIN LOOP=================
while True:
menu()
temp=ent_temp("Enter Temperature:")
unit=ent_unit("Enter the source unit (C/K/R/F):")
target=ent_unit("Enter target unit (C/K/R/F):")
result=convert(temp,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