-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit_converter.py
More file actions
134 lines (110 loc) · 3.67 KB
/
unit_converter.py
File metadata and controls
134 lines (110 loc) · 3.67 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
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
#!/usr/bin/env python3
"""
Unit Converter CLI Tool
Supports: Length, Weight, and Temperature conversions
"""
def convert_length(value, from_unit, to_unit):
"""Convert between length units"""
# Convert to meters first
to_meters = {
'mm': 0.001,
'cm': 0.01,
'm': 1,
'km': 1000,
'inch': 0.0254,
'ft': 0.3048,
'yard': 0.9144,
'mile': 1609.34
}
meters = value * to_meters[from_unit]
return meters / to_meters[to_unit]
def convert_weight(value, from_unit, to_unit):
"""Convert between weight units"""
# Convert to kilograms first
to_kg = {
'mg': 0.000001,
'g': 0.001,
'kg': 1,
'ton': 1000,
'oz': 0.0283495,
'lb': 0.453592
}
kg = value * to_kg[from_unit]
return kg / to_kg[to_unit]
def convert_temperature(value, from_unit, to_unit):
"""Convert between temperature units"""
# Convert to Celsius first
if from_unit == 'c':
celsius = value
elif from_unit == 'f':
celsius = (value - 32) * 5/9
elif from_unit == 'k':
celsius = value - 273.15
# Convert from Celsius to target
if to_unit == 'c':
return celsius
elif to_unit == 'f':
return celsius * 9/5 + 32
elif to_unit == 'k':
return celsius + 273.15
def display_menu():
"""Display main menu"""
print("\n" + "="*50)
print(" UNIT CONVERTER CLI TOOL")
print("="*50)
print("\n1. Length Conversion")
print("2. Weight Conversion")
print("3. Temperature Conversion")
print("4. Exit")
print("-"*50)
def length_menu():
"""Handle length conversion"""
print("\nLength Units: mm, cm, m, km, inch, ft, yard, mile")
try:
value = float(input("Enter value: "))
from_unit = input("From unit: ").lower()
to_unit = input("To unit: ").lower()
result = convert_length(value, from_unit, to_unit)
print(f"\n✓ {value} {from_unit} = {result:.4f} {to_unit}")
except (KeyError, ValueError) as e:
print(f"\n✗ Error: Invalid input. Please check units and value.")
def weight_menu():
"""Handle weight conversion"""
print("\nWeight Units: mg, g, kg, ton, oz, lb")
try:
value = float(input("Enter value: "))
from_unit = input("From unit: ").lower()
to_unit = input("To unit: ").lower()
result = convert_weight(value, from_unit, to_unit)
print(f"\n✓ {value} {from_unit} = {result:.4f} {to_unit}")
except (KeyError, ValueError) as e:
print(f"\n✗ Error: Invalid input. Please check units and value.")
def temperature_menu():
"""Handle temperature conversion"""
print("\nTemperature Units: c (Celsius), f (Fahrenheit), k (Kelvin)")
try:
value = float(input("Enter value: "))
from_unit = input("From unit: ").lower()
to_unit = input("To unit: ").lower()
result = convert_temperature(value, from_unit, to_unit)
print(f"\n✓ {value}°{from_unit.upper()} = {result:.2f}°{to_unit.upper()}")
except (KeyError, ValueError) as e:
print(f"\n✗ Error: Invalid input. Please check units and value.")
def main():
"""Main program loop"""
while True:
display_menu()
choice = input("Select option (1-4): ")
if choice == '1':
length_menu()
elif choice == '2':
weight_menu()
elif choice == '3':
temperature_menu()
elif choice == '4':
print("\nThank you for using Unit Converter!")
break
else:
print("\n✗ Invalid choice. Please select 1-4.")
if __name__ == "__main__":
main()