Sunday, March 29, 2015

Fahrenheit To Celsius Conversion Program in Python - Python Tutorial

Fahrenheit To Celsius Conversion Program in PythonFahrenheit To Celsius Conversion Program


In this tutorial, we are going to build a simple Fahrenheit to Celsius Conversion Program in Python. This program is similar to our previous program with some slight changes to perform fahrenheit to celsius conversion.


Functions and Methods Used 


  • printt()

  • float()

  • input()

  • round()

  • .format()

Step 1 – Open Text Editor and Save Program


In the first step, we need to open your preferred text editor we use Sublime Text and save your program to your desktop as ftoc.py.


Step 2 – Write a welcome to our users


In this step, we are going to write a simple welcome statement to our users using a print statement.


#Write a welcome to our users

print("Welcome to Fahrenheit to Celsius Program")

Step 3 – User’s Input


First we need to create a variable in this step we created a variable called “fah” for fahrenheit.  Then we need to convert the user’s input to a floating point number using float().  Then we prompt the user for a number using the input() function.


#User's Input

print("Welcome to Fahrenheit to Celsius Program")
fah = float(input("Please give us a temperature in Fahrenheit. "))

Step 4 – The Conversion


In this step, we have to create a variable to point to the value we get from our conversion. We create a variable called “cel” for celsius. We will need to round this equation so we put the equation in the round() function with one number after the decimal. Then we have to put the first part of the equation in parentheses so this part is performed before any other part of the equation (fah – 32). Then we need in order to finish off the equation by multiplying by 5 and then divide by 9.


#The Equation

print("Welcome to Fahrenheit to Celsius Program")
fah = float(input("Please give us a temperature in Fahrenheit. "))
cel = round((fah - 32) * 5 / 9, 1)

Step 5 – Return the conversion to the user


We return to the conversion to the user using a print() statement which we .format() to include our variable “cel” which will be replaced by the value of our conversion.


#Return Conversion To User

print("Welcome to Fahrenheit to Celsius Program")
fah = float(input("Please give us a temperature in Fahrenheit. "))
cel = round((fah - 32) * 5 / 9, 1)
print("The temperature in Celsius is ".format(cel))

Step 6 – Run the Program


To run the program open your terminal or command prompt.  Cd into your desktop directory and enter python3 ftoc.py


#Run The Program

Thomass-MBP:desktop Tommy$ python3 ftoc.py
Welcome to Fahrenheit to Celsius Program
Please give us a temperature in Fahrenheit. 78
The temperature in Celsius is 25.6

In this Python Programming Tutorial, we created a Fahrenheit to Celsius Conversion Program in Python. If you have any questions please leave a comment below and we will help you out.


 



No comments:

Post a Comment