Python Program for Temperature Converter

In this tutorial, we will discuss a Python program for temperature converter.

We will see about the temperature converter that can convert temperatures between Celsius and Fahrenheit using a Python program.

Related: Python Program to Check if a String or Number is a Palindrome

Program code for temperature converter in Python

# Temperature Converter in Python
def celsius_to_fahrenheit(celsius):
    return (celsius * 9/5) + 32

def fahrenheit_to_celsius(fahrenheit):
    return (fahrenheit - 32) * 5/9

print("Select conversion:")
print("1. Celsius to Fahrenheit")
print("2. Fahrenheit to Celsius")

choice = input("Enter choice(1/2): ")

if choice == '1':
    celsius = float(input("Enter temperature in Celsius: "))
    print(f"{celsius} Celsius is equal to {celsius_to_fahrenheit(celsius)} Fahrenheit.")
elif choice == '2':
    fahrenheit = float(input("Enter temperature in Fahrenheit: "))
    print(f"{fahrenheit} Fahrenheit is equal to {fahrenheit_to_celsius(fahrenheit)} Celsius.")
else:
    print("Invalid input")

Explanation

  1. Function Definitions: The program defines two functions: celsius_to_fahrenheit and fahrenheit_to_celsius to perform the respective temperature conversions.
  2. Main Program: The program prompts the user to select a conversion and enter the temperature. Based on the user’s choice, it performs the selected temperature conversion and prints the result.

Output

Python Program for Temperature Converter

Conclusion

  • In this tutorial, we learned about temperature converter using python.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *