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
- Function Definitions: The program defines two functions:
celsius_to_fahrenheit
andfahrenheit_to_celsius
to perform the respective temperature conversions. - 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
Conclusion
- In this tutorial, we learned about temperature converter using python.