Saturday, March 28, 2015

Convert Celsius to Fahrenheit Python Program tutorial - Python Tutorial

Convert Celsius to Fahrenheit Python Program tutorialConvert Celsius to Fahrenheit Python Program tutorial


In this Python tutorial, we are going to build a Python program that will convert celsius to fahrenheit. This is another very simple program that takes the users input and converts celsius temperature to a fahrenheit temperature and prints the temperature back to the user.


Functions and Methods Used in This Tutorial


  • print()

  • float()

  • .format()

Build The Python Program


In this program, we will be using Python 3, a text editor of your choice we use Sublime Text and the Python interpreter.


Step 1 – Open Text Editor and Save Program


Open your text editor and save your file to your desktop as ctof.py(Celsius to Fahrenheit). The dot py is a Python extension.


Step 2 – Welcome Message to Users


We want to just add a very simple welcome message to the users.  To this we use the print() function.


#Welcome Message to Users

#Spelled celsius and Fahrenheit wrong sorry!
print("Convert Celcius to Fahreheit")

Step 3 – Retrieve the users input


To retrieve the user’s input we first need to assign a variable to that object the user will create. In this step, we create a variable called “cel” for celsius. We then need in order to convert the number to a floating point number using the float() function in Python. Then we use the input() function to get the users temperature in celsius.  We also prompt the user for the input() function by adding a string “Input temperature in celsius”.


#Retrieve the users input

print("Convert Celcius to Fahreheit")
cel = float(input("Input tempature in celcius. "))

Step 4 – The conversion


In this step, we need to assign a variable to the value of our equation. We create a variable called “fah” and assign the returned value from our conversion. To convert celsius to fahrenheit we first need to input the user’s value by using the variable called “cel” then multiply that number by 9 then divide by 5 and then add 32 to get the temperature in fahrenheit.


#The conversion

print("Convert Celcius to Fahreheit")
cel = float(input("Input tempature in celcius. "))
fah = cel * 9 / 5 + 32

Step 5 – Print conversion to user


In this step, we need to return the conversion to the user. We do this using a print() statement and use .format() method to convert the string.


#Print Conversion To User

print("Convert Celcius to Fahreheit")
cel = float(input("Input tempature in celcius. "))
fah = cel * 9 / 5 + 32
print("The tempature in Fahreheit is ".format(fah))

Step 6 – Save and Run the program


Save your program and we need to run the program.  To run the program we will go into our terminal or command prompt and cd into our desktop and type python3 ctof.py and press return.


#Save and Run the program

Thomass-MBP:desktop Tommy$ python3 ctof.py
Convert Celcius to Fahreheit
Input tempature in celcius. 22
The tempature in Fahreheit is 71.6

Conclusion 


In this Python tutorial, we built a very simple program that will convert celsius to fahrenheit and return the value to the user. If you have any questions please leave a comment below and we will help you out.


 


Source Code

No comments:

Post a Comment