-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp_conversion_tool.py
More file actions
24 lines (20 loc) · 862 Bytes
/
Copy pathtemp_conversion_tool.py
File metadata and controls
24 lines (20 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
FAHRENHEIT_TO_CELSIUS_FACTOR = 5 / 9
CELSIUS_TO_FAHRENHEIT_FACTOR = 9 / 5
def convert_to_celsius(fahrenheit):
return (fahrenheit - 32) * FAHRENHEIT_TO_CELSIUS_FACTOR
def convert_to_fahrenheit(celsius):
return (celsius * CELSIUS_TO_FAHRENHEIT_FACTOR) + 32
try:
temp_input = input("Enter the temperature to convert: ")
temperature = float(temp_input)
unit = input("Is this temperature in Celsius or Fahrenheit? (C/F): ").strip().upper()
if unit == 'C':
result = convert_to_fahrenheit(temperature)
print(f"{temperature}°C is {result}°F")
elif unit == 'F':
result = convert_to_celsius(temperature)
print(f"{temperature}°F is {result}°C")
else:
print("Invalid unit. Please enter 'C' or 'F'.")
except ValueError:
raise ValueError("Invalid temperature. Please enter a numeric value.")