forked from Sureshrayirath/Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit Converter.py
More file actions
65 lines (49 loc) · 1.85 KB
/
Unit Converter.py
File metadata and controls
65 lines (49 loc) · 1.85 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
# unit_converter.py
def km_to_miles(km):
return km * 0.621371
def miles_to_km(miles):
return miles / 0.621371
def celsius_to_fahrenheit(c):
return (c * 9/5) + 32
def fahrenheit_to_celsius(f):
return (f - 32) * 5/9
def kg_to_pounds(kg):
return kg * 2.20462
def pounds_to_kg(lb):
return lb / 2.20462
def main():
print("----- Unit Converter -----")
print("1. Kilometers ↔ Miles")
print("2. Celsius ↔ Fahrenheit")
print("3. Kilograms ↔ Pounds")
print("0. Exit")
while True:
choice = input("\nChoose an option (0-3): ")
if choice == "1":
val = float(input("Enter value: "))
direction = input("Convert to (m for miles, k for km): ").lower()
if direction == "m":
print(f"{val} km = {km_to_miles(val):.2f} miles")
else:
print(f"{val} miles = {miles_to_km(val):.2f} km")
elif choice == "2":
val = float(input("Enter value: "))
direction = input("Convert to (f for Fahrenheit, c for Celsius): ").lower()
if direction == "f":
print(f"{val}°C = {celsius_to_fahrenheit(val):.2f}°F")
else:
print(f"{val}°F = {fahrenheit_to_celsius(val):.2f}°C")
elif choice == "3":
val = float(input("Enter value: "))
direction = input("Convert to (p for pounds, k for kg): ").lower()
if direction == "p":
print(f"{val} kg = {kg_to_pounds(val):.2f} pounds")
else:
print(f"{val} pounds = {pounds_to_kg(val):.2f} kg")
elif choice == "0":
print("Exiting... Goodbye!")
break
else:
print("Invalid choice! Please try again.")
if __name__ == "__main__":
main()