Python
Python

 

 

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:

  1. Ensure you have Python installed on your computer. You can download it from here.
  2. Create a new Python file (e.g., temperature_converter.py) using any text editor or Integrated Development Environment (IDE).
  3. Copy and paste the provided code into the Python file.
  4. Open the terminal or command prompt and navigate to the directory where your Python file is located.
  5. Run the program by typing: python temperature_converter.py (or python3 temperature_converter.py if you are using a Mac or Linux system).
  6. 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.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)