Kalebu Jordan

Become a Pro Python Developer

On this tutorial you’re going to learn how to build a program to Convert Fahrenheit temperature to Centigrade temperature and vice verse .

Program overview

Our program will have one simple menu for a user to select whether he/she wants to convert Fahrenheit to Centigrade or Centigrade to Fahrenheit.

And then after that We are going to perform the conversion based on choice made by user

Program Instructions summary

  • Displaying option for user to choose whether F -> C or C->F
  • If F->C , performing conversion of F – > C and vice verse
  • displaying the converted value to the screen

General Formula

The below are general formula for Temperature conversion

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

Now Let’s build our converter Program

Converter.py

#Displaying choice menu to user
print('Which conversion do you want ?')
print('1.Fahrenheit to Celsius\n2.Celsius to Fahrenheit')
option = int(input('Enter option here : '))

if option == 1:
    Fahrenheit = float(input('Enter Temperature in Fahrenheit:  '))
    Celsius = (Fahrenheit - 32) * 5/9
    print('Temperature in Celsius will be ', Celsius)
elif option == 2:
    Celsius = float(input('Enter Temperature in Celsius: '))
    Fahrenheit = (Celsius * 9/5) + 32
    print('Temperature in Fahrenheit will be ', Fahrenheit)
else:
    print('Invalid option please try again later')

Running Our Program

kalebu@kalebu-PC:~$ python3 converter.py 
Which conversion do you want ?
1.Fahrenheit to Celsius
2.Celsius to Fahrenheit
Enter option here : 1
Enter Temperature in Fahrenheit:  90
Temperature in Celsius will be  32.22222222222222
---------------------------------------------------

kalebu@kalebu-PC:~$ python3 converter.py 
Which conversion do you want ?
1.Fahrenheit to Celsius
2.Celsius to Fahrenheit
Enter option here : 2
Enter Temperature in Celsius: 40
Temperature in Fahrenheit will be  104.0
--------------------------------------------------

kalebu@kalebu-PC:~$ python3 converter.py 
Which conversion do you want ?
1.Fahrenheit to Celsius
2.Celsius to Fahrenheit
Enter option here : 4
Invalid option please try again later

Hope you find this post interesting , don’t forget to subscribe to get more tutorials like this

In case of any suggestion or comment , drop it on the comment box and I will reply to you immediately.

Leave a Reply

error

Enjoy this blog? Please spread the word :)