-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathUnit Converter
73 lines (64 loc) · 2.66 KB
/
Unit Converter
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
def convert_length(value, from_unit, to_unit):
# Define conversion factors
length_factors = {
"meters_to_feet": 3.28084,
"feet_to_meters": 0.3048,
"meters_to_miles": 0.000621371,
# Add more conversion factors as needed
}
if from_unit == to_unit:
return value # No conversion needed
else:
conversion_key = f"{from_unit}_to_{to_unit}"
if conversion_key in length_factors:
return value * length_factors[conversion_key]
else:
return "Invalid conversion units"
def convert_weight(value, from_unit, to_unit):
# Define conversion factors
weight_factors = {
"kilograms_to_pounds": 2.20462,
"pounds_to_kilograms": 0.453592,
# Add more conversion factors as needed
}
if from_unit == to_unit:
return value # No conversion needed
else:
conversion_key = f"{from_unit}_to_{to_unit}"
if conversion_key in weight_factors:
return value * weight_factors[conversion_key]
else:
return "Invalid conversion units"
def convert_temperature(value, from_unit, to_unit):
if from_unit == "celsius" and to_unit == "fahrenheit":
return (value * 9/5) + 32
elif from_unit == "fahrenheit" and to_unit == "celsius":
return (value - 32) * 5/9
else:
return "Invalid conversion units"
if __name__ == "__main__":
print("Unit Converter")
print("1. Length")
print("2. Weight")
print("3. Temperature")
choice = int(input("Select a conversion type (1/2/3): "))
if choice == 1:
value = float(input("Enter the length value: "))
from_unit = input("Enter the source unit (e.g., meters): ").lower()
to_unit = input("Enter the target unit (e.g., feet): ").lower()
result = convert_length(value, from_unit, to_unit)
print(f"Result: {value} {from_unit} = {result} {to_unit}")
elif choice == 2:
value = float(input("Enter the weight value: "))
from_unit = input("Enter the source unit (e.g., kilograms): ").lower()
to_unit = input("Enter the target unit (e.g., pounds): ").lower()
result = convert_weight(value, from_unit, to_unit)
print(f"Result: {value} {from_unit} = {result} {to_unit}")
elif choice == 3:
value = float(input("Enter the temperature value: "))
from_unit = input("Enter the source unit (celsius/fahrenheit): ").lower()
to_unit = input("Enter the target unit (celsius/fahrenheit): ").lower()
result = convert_temperature(value, from_unit, to_unit)
print(f"Result: {value} {from_unit} = {result} {to_unit}")
else:
print("Invalid choice")