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/9
Celsius = (Fahrenheit - 32) * 5/9
Fahrenheit = (Celsius * 9/5) + 32
Fahrenheit = (Celsius * 9/5) + 32
Prerequisites
- Python 3.6 or above
- Text Editor or IDE
Getting Started
Creating a new project
- Create a new project folder and name it
tempconverter
tempconverter
. - Create a new file inside the
tempconverter
tempconverter
folder and name ittempconverter.py
tempconverter.py
. - Open the project folder in your favorite text editor or IDE.
- Copy the code below and paste it into the
tempconverter.py
tempconverter.py
file.
Write the code
- Add the following code to the
tempconverter.py
tempconverter.py
file.
⚙️ 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
# 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()
- Save the file.
- Open the terminal and navigate to the project folder.
- Run the following command to execute the program.
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
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 thecelsius_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 thefahrenheit_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
- Open the terminal and navigate to the project folder.
- Run the following command to execute the program.
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
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.
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
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.py
tempconverter.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:
- Modify the program to convert temperature from Kelvin to Celsius and vice versa.
- Modify the program to convert temperature from Kelvin to Fahrenheit and vice versa.
- Modify the program to convert temperature from Celsius to Kelvin and vice versa.
- Modify the program to convert temperature from Fahrenheit to Kelvin and vice versa.
- Modify the program to convert temperature from Kelvin to Rankine and vice versa.
- Modify the program to convert temperature from Rankine to Kelvin and vice versa.
- Modify the program to convert temperature from Celsius to Rankine and vice versa.
- Modify the program to convert temperature from Rankine to Celsius and vice versa.
- Modify the program to convert temperature from Fahrenheit to Rankine and vice versa.
- 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