Introduction
Temperature conversion is a common operation that is useful in various real-world applications. It allows us to convert temperature readings between different units of measurement. The most commonly used temperature scales are Celsius (°C) and Fahrenheit (°F).
Objective
The objective of this program is to create a simple Python application that allows the user to convert temperatures between Celsius and Fahrenheit. The user will be able to input a temperature value and choose the direction of conversion, either from Celsius to Fahrenheit or from Fahrenheit to Celsius.
Python Code for Temperature Conversion
# Function to convert Celsius to Fahrenheit
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
# Function to convert Fahrenheit to Celsius
def fahrenheit_to_celsius(fahrenheit):
return (fahrenheit - 32) * 5/9
# Main function to perform the conversion
def main():
print("Temperature Conversion Program")
print("1. Celsius to Fahrenheit")
print("2. Fahrenheit to Celsius")
choice = int(input("Enter the choice (1 or 2): "))
if choice == 1:
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = celsius_to_fahrenheit(celsius)
print(f"{celsius}°C is equal to {fahrenheit}°F")
elif choice == 2:
fahrenheit = float(input("Enter temperature in Fahrenheit: "))
celsius = fahrenheit_to_celsius(fahrenheit)
print(f"{fahrenheit}°F is equal to {celsius}°C")
else:
print("Invalid choice! Please select either 1 or 2.")
if __name__ == "__main__":
main()
Explanation of the Program Structure
This program consists of the following parts:
- celsius_to_fahrenheit(celsius): This function takes a temperature in Celsius as input and returns the equivalent temperature in Fahrenheit using the formula:
F = (C * 9/5) + 32
. - fahrenheit_to_celsius(fahrenheit): This function takes a temperature in Fahrenheit as input and returns the equivalent temperature in Celsius using the formula:
C = (F - 32) * 5/9
. - main(): This is the main function that runs the program. It displays the menu options for the user, accepts their input for the desired conversion, and calls the appropriate function based on the user’s choice. It also handles invalid inputs gracefully by displaying an error message if an invalid option is chosen.
How to Run the Program
Follow these steps to run the program:
- Ensure you have Python installed on your computer. You can download it from here.
- Create a new Python file (e.g.,
temperature_converter.py
) using any text editor or Integrated Development Environment (IDE). - Copy and paste the provided code into the Python file.
- Open the terminal or command prompt and navigate to the directory where your Python file is located.
- Run the program by typing:
python temperature_converter.py
(orpython3 temperature_converter.py
if you are using a Mac or Linux system). - Follow the on-screen instructions to convert temperatures between Celsius and Fahrenheit.
Conclusion
This simple temperature conversion program demonstrates how Python can be used to solve real-world problems like temperature conversion. It also introduces the use of functions to organize code and makes it more readable and reusable.