diff --git a/Convert Temperature/Convert Temperature.py b/Convert Temperature/Convert Temperature.py index 51aa0f51..78659451 100644 --- a/Convert Temperature/Convert Temperature.py +++ b/Convert Temperature/Convert Temperature.py @@ -1,67 +1,58 @@ -import math - -def from_cel(): - x = float(input("Enter Temperature: ")) - y = input("You want to convert it into: \n (a)fahrenheit (b)Kelvin ") - - if y == 'a': - final = (x*(9/5)) + 32 - print(f"The temperature is: {final} °F") - - else: - final = x + 273.15 - print(f"The temperature is: {final} K") - - - -def from_fah(): - x = float(input("Enter Temperature: ")) - y = input("You want to convert it into: \n (a)celsius (b)Kelvin ") - - if y == 'a': - final = (x - 32)*(5/9) - print(f"The temperature is: {final} °C") - - - else: - final = (x - 32)*(5/9) + 273.15 - print(f"The temperature is: {final} K") - - -def from_Kel(): - x = float(input("Enter Temperature: ")) - y = input("You want to convert it into: \n (a)celsius (b)fahrenheit ") - - if y == 'a': - final = x - 273.15 - print(f"The temperature is: {final} °C") +def convert_from_celsius(temp: float, to_unit: str) -> float: + if to_unit == 'a': + return (temp * 9 / 5) + 32 # Celsius to Fahrenheit + elif to_unit == 'b': + return temp + 273.15 # Celsius to Kelvin + + +def convert_from_fahrenheit(temp: float, to_unit: str) -> float: + if to_unit == 'a': + return (temp - 32) * 5 / 9 # Fahrenheit to Celsius + elif to_unit == 'b': + return (temp - 32) * 5 / 9 + 273.15 # Fahrenheit to Kelvin + + +def convert_from_kelvin(temp: float, to_unit: str) -> float: + if to_unit == 'a': + return temp - 273.15 # Kelvin to Celsius + elif to_unit == 'b': + return (temp - 273.15) * 9 / 5 + 32 # Kelvin to Fahrenheit + + +def get_conversion(): + print("Choose the input temperature scale:") + print("1. Celsius") + print("2. Fahrenheit") + print("3. Kelvin") + from_unit = input("Enter option (1/2/3): ") + + try: + temp = float(input("Enter the temperature: ")) + except ValueError: + print("Invalid temperature input.") + return + + if from_unit == "1": + to_unit = input("Convert to:\n(a) Fahrenheit\n(b) Kelvin\nEnter a/b: ") + result = convert_from_celsius(temp, to_unit) + suffix = "°F" if to_unit == 'a' else "K" + + elif from_unit == "2": + to_unit = input("Convert to:\n(a) Celsius\n(b) Kelvin\nEnter a/b: ") + result = convert_from_fahrenheit(temp, to_unit) + suffix = "°C" if to_unit == 'a' else "K" + + elif from_unit == "3": + to_unit = input("Convert to:\n(a) Celsius\n(b) Fahrenheit\nEnter a/b: ") + result = convert_from_kelvin(temp, to_unit) + suffix = "°C" if to_unit == 'a' else "°F" else: - final = (x - 273.15)*(5/9) + 32 - print(f"The temperature is: {final} °F") - - - - -def get_temp(): - global t - t = input("What would you like to convert from? (input no.): \n (1)Celsius (2)fahrenheit (3)Kelvin ") - -get_temp() - - -if t == "1": - from_cel() - -elif t == '2': - from_fah() - -elif t == '3': - from_Kel() - -else: - print("please enter a correct input") - get_temp() + print("Invalid choice. Please choose 1, 2, or 3.") + return + print(f"Converted temperature: {result:.2f} {suffix}") +if __name__ == "__main__": + get_conversion()