Skip to content

Temperature Converter

Abstract

This program converts temperature from Fahrenheit to Celsius and vice versa. This program is written in Python. This is a command line program. This application work on the following formulae:

Celsius = (Fahrenheit - 32) * 5/9Celsius = (Fahrenheit - 32) * 5/9

Fahrenheit = (Celsius * 9/5) + 32Fahrenheit = (Celsius * 9/5) + 32

Prerequisites

  • Python 3.6 or above
  • Text Editor or IDE

Getting Started

Creating a new project

  1. Create a new project folder and name it tempconvertertempconverter.
  2. Create a new file inside the tempconvertertempconverter folder and name it tempconverter.pytempconverter.py.
  3. Open the project folder in your favorite text editor or IDE.
  4. Copy the code below and paste it into the tempconverter.pytempconverter.py file.

Write the code

  1. Add the following code to the tempconverter.pytempconverter.py file.
⚙️ Temperature Converter
Temperature Converter
# Temperature Converter
 
# Function to convert Celsius to Fahrenheit
def celsius_to_fahrenheit(celsius):
    fahrenheit = (celsius * 9/5) + 32
    return fahrenheit
 
# Function to convert Fahrenheit to Celsius
def fahrenheit_to_celsius(fahrenheit):
    celsius = (fahrenheit - 32) * 5/9
    return celsius
 
# Main function
def main():
    print("Temperature Converter")
    print("1. Celsius to Fahrenheit")
    print("2. Fahrenheit to Celsius")
    choice = int(input("Enter your choice: "))
    if choice == 1:
        celsius = float(input("Enter temperature in Celsius: "))
        fahrenheit = celsius_to_fahrenheit(celsius)
        print("Temperature in Fahrenheit: ", fahrenheit)
    elif choice == 2:
        fahrenheit = float(input("Enter temperature in Fahrenheit: "))
        celsius = fahrenheit_to_celsius(fahrenheit)
        print("Temperature in Celsius: ", celsius)
    else:
        print("Invalid choice!")
        
# Call main function
main()
 
Temperature Converter
# Temperature Converter
 
# Function to convert Celsius to Fahrenheit
def celsius_to_fahrenheit(celsius):
    fahrenheit = (celsius * 9/5) + 32
    return fahrenheit
 
# Function to convert Fahrenheit to Celsius
def fahrenheit_to_celsius(fahrenheit):
    celsius = (fahrenheit - 32) * 5/9
    return celsius
 
# Main function
def main():
    print("Temperature Converter")
    print("1. Celsius to Fahrenheit")
    print("2. Fahrenheit to Celsius")
    choice = int(input("Enter your choice: "))
    if choice == 1:
        celsius = float(input("Enter temperature in Celsius: "))
        fahrenheit = celsius_to_fahrenheit(celsius)
        print("Temperature in Fahrenheit: ", fahrenheit)
    elif choice == 2:
        fahrenheit = float(input("Enter temperature in Fahrenheit: "))
        celsius = fahrenheit_to_celsius(fahrenheit)
        print("Temperature in Celsius: ", celsius)
    else:
        print("Invalid choice!")
        
# Call main function
main()
 
  1. Save the file.
  2. Open the terminal and navigate to the project folder.
  3. Run the following command to execute the program.
command
C:\Users\username\Documents\tempconverter> python tempconverter.py 
Temperature Converter
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
Enter your choice: 1
Enter temperature in Celsius: 35
Temperature in Fahrenheit:  95.0
command
C:\Users\username\Documents\tempconverter> python tempconverter.py 
Temperature Converter
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
Enter your choice: 1
Enter temperature in Celsius: 35
Temperature in Fahrenheit:  95.0

Explanation

  • The celsius_to_fahrenheit()celsius_to_fahrenheit() function takes a temperature in Celsius as an argument and returns the temperature in Fahrenheit.
  • The fahrenheit_to_celsius()fahrenheit_to_celsius() function takes a temperature in Fahrenheit as an argument and returns the temperature in Celsius.
  • The main()main() function displays a menu to the user and asks the user to enter a choice. If the user enters 1, the program asks the user to enter a temperature in Celsius and calls the celsius_to_fahrenheit()celsius_to_fahrenheit() function to convert the temperature from Celsius to Fahrenheit. If the user enters 2, the program asks the user to enter a temperature in Fahrenheit and calls the fahrenheit_to_celsius()fahrenheit_to_celsius() function to convert the temperature from Fahrenheit to Celsius. If the user enters any other number, the program displays an error message.
  • The main()main() function is called at the end of the program.

Usage

  1. Open the terminal and navigate to the project folder.
  2. Run the following command to execute the program.
command
C:\Users\username\Documents\tempconverter> python tempconverter.py
Temperature Converter
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
Enter your choice: 1
Enter temperature in Celsius: 35
Temperature in Fahrenheit:  95.0
command
C:\Users\username\Documents\tempconverter> python tempconverter.py
Temperature Converter
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
Enter your choice: 1
Enter temperature in Celsius: 35
Temperature in Fahrenheit:  95.0

In this example, the user enters 1 to convert temperature from Celsius to Fahrenheit. The user enters 35 as the temperature in Celsius. The program calls the celsius_to_fahrenheit()celsius_to_fahrenheit() function to convert the temperature from Celsius to Fahrenheit and displays the result. 3. You can also use the program to convert temperature from Fahrenheit to Celsius.

command
C:\Users\username\Documents\tempconverter> python tempconverter.py
Temperature Converter
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
Enter your choice: 2
Enter temperature in Fahrenheit: 95
Temperature in Celsius:  35.0
command
C:\Users\username\Documents\tempconverter> python tempconverter.py
Temperature Converter
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
Enter your choice: 2
Enter temperature in Fahrenheit: 95
Temperature in Celsius:  35.0

In this example, the user enters 2 to convert temperature from Fahrenheit to Celsius. The user enters 95 as the temperature in Fahrenheit. The program calls the fahrenheit_to_celsius()fahrenheit_to_celsius() function to convert the temperature from Fahrenheit to Celsius and displays the result. 4. You can also run the program by double-clicking the tempconverter.pytempconverter.py file.

Next Steps

Congratulations! You have successfully created a temperature converter program in Python. You can now use this program to convert temperature from Celsius to Fahrenheit and vice versa.

Here are some ideas to try:

  1. Modify the program to convert temperature from Kelvin to Celsius and vice versa.
  2. Modify the program to convert temperature from Kelvin to Fahrenheit and vice versa.
  3. Modify the program to convert temperature from Celsius to Kelvin and vice versa.
  4. Modify the program to convert temperature from Fahrenheit to Kelvin and vice versa.
  5. Modify the program to convert temperature from Kelvin to Rankine and vice versa.
  6. Modify the program to convert temperature from Rankine to Kelvin and vice versa.
  7. Modify the program to convert temperature from Celsius to Rankine and vice versa.
  8. Modify the program to convert temperature from Rankine to Celsius and vice versa.
  9. Modify the program to convert temperature from Fahrenheit to Rankine and vice versa.
  10. Modify the program to convert temperature from Rankine to Fahrenheit and vice versa.

Conclusion

In this tutorial, you learned how to create a temperature converter program in Python. You also learned how to convert temperature from Celsius to Fahrenheit and vice versa. You can now use this program to convert temperature from Celsius to Fahrenheit and vice versa.

You also learned how to use programming to perform calculations. You can use this knowledge to create more programs that perform calculations.

Keep learning and keep building awesome stuff. If you have any questions or feedback, please feel free to send me a message on Website. I would love to hear from you. Learn more about tutorial and projects on Python Central Hub.

Was this page helpful?

Let us know how we did