-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path26_unit_converter.py
More file actions
74 lines (59 loc) · 2.09 KB
/
26_unit_converter.py
File metadata and controls
74 lines (59 loc) · 2.09 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
# 26. Unit Converter
def length_conversion(value, from_unit, to_unit):
# Base unit: Meter
conversions = {
'meters': 1,
'feet': 3.28084,
'inches': 39.3701,
'kilometers': 0.001,
'miles': 0.000621371
}
if from_unit not in conversions or to_unit not in conversions:
return None
# Convert from input unit to meters
meters = value / conversions[from_unit]
# Convert from meters to target unit
result = meters * conversions[to_unit]
return result
def weight_conversion(value, from_unit, to_unit):
# Base unit: Kilogram
conversions = {
'kilograms': 1,
'pounds': 2.20462,
'ounces': 35.274,
'grams': 1000
}
if from_unit not in conversions or to_unit not in conversions:
return None
# Convert from input unit to kilograms
kilograms = value / conversions[from_unit]
# Convert from kilograms to target unit
result = kilograms * conversions[to_unit]
return result
def main():
print("=== Unit Converter ===")
print("1. Length (meters, feet, inches, kilometers, miles)")
print("2. Weight (kilograms, pounds, ounces, grams)")
choice = input("Select a conversion category (1-2): ")
if choice == '1':
value = float(input("Enter Value: "))
from_unit = input("Convert from unit: ").lower()
to_unit = input("Convert to unit: ").lower()
result = length_conversion(value, from_unit, to_unit)
if result is not None:
print(f"{value} {from_unit} is equal to {result:.2f} {to_unit}.")
else:
print("Invalid units.")
elif choice == '2':
value = float(input("Enter Value: "))
from_unit = input("Convert from unit: ").lower()
to_unit = input("Convert to unit: ").lower()
result = weight_conversion(value, from_unit, to_unit)
if result is not None:
print(f"{value} {from_unit} is equal to {result:.2f} {to_unit}.")
else:
print("Invalid units.")
else:
print("Invalid category.")
if __name__ == "__main__":
main()