-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunitconvertor.py
69 lines (56 loc) · 2.35 KB
/
unitconvertor.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
from colorama import init, Fore, Style
from tqdm import tqdm
import time
# Initialize colorama
init(autoreset=True)
def show_progress():
"""Simulates a loading bar for conversions."""
print(Fore.CYAN + "Processing your conversion... 🚀")
for _ in tqdm(range(100), desc="Converting", ascii=True, colour="green"):
time.sleep(0.01)
def unit_converter():
print(Style.BRIGHT + Fore.YELLOW + "Welcome to the Unit Converter!")
print(Fore.CYAN + "Choose the type of conversion you'd like to perform:")
# Conversion options
options = {
1: "Kilometers to Miles",
2: "Miles to Kilometers",
3: "Celsius to Fahrenheit",
4: "Fahrenheit to Celsius"
}
# Display options
for key, value in options.items():
print(f"{key}. {value}")
try:
choice = int(input(Fore.YELLOW + "\nEnter the number of your choice: "))
if choice in options:
value = float(input(Fore.GREEN + f"Enter the value to convert ({options[choice]}): "))
# Show progress bar
show_progress()
if choice == 1: # Kilometers to Miles
result = value * 0.621371
unit_from, unit_to = "Kilometers", "Miles"
elif choice == 2: # Miles to Kilometers
result = value / 0.621371
unit_from, unit_to = "Miles", "Kilometers"
elif choice == 3: # Celsius to Fahrenheit
result = (value * 9/5) + 32
unit_from, unit_to = "Celsius", "Fahrenheit"
elif choice == 4: # Fahrenheit to Celsius
result = (value - 32) * 5/9
unit_from, unit_to = "Fahrenheit", "Celsius"
print(Fore.GREEN + f"\n✅ Conversion Completed!")
print(Fore.YELLOW + f"{value} {unit_from} = {result:.2f} {unit_to}")
else:
print(Fore.RED + "Invalid choice! Please select a valid option.")
except ValueError:
print(Fore.RED + "Invalid input! Please enter a numeric value.")
def main():
while True:
unit_converter()
retry = input(Fore.CYAN + "\nDo you want to perform another conversion? (yes/no): ").strip().lower()
if retry != "yes":
print(Fore.YELLOW + "Thank you for using the Unit Converter. Goodbye! 👋")
break
if __name__ == "__main__":
main()