In this lesson you are going to learn how to convert Fahrenheit to Celsius by using Python script, I’m following Python 3 syntax to write programs which actually works in both Python 2 and Python 3.
Formula for finding the Celsius:
celsius = (fahrenheit – 32)*5/9
Program is very simple only 4 lines of script:
print("Enter Fahrenheit")
#get the fahrenheit
fahrenheit = float(input())
#convert fahrenheit to celsius
celsius = (fahrenheit - 32)*5/9
print ("Celsius:", celsius)
Output:
Enter Fahrenheit: 100
Celsius: 37.7779
The above program is a console program, the same script also can be written as a Python Web script.
Here is the Python web application script to convert Fahrenheit to Celsius:
#!/usr/bin/python
print "Content-type: text/html\n\n";
# Import modules for CGI handling
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
number = form.getvalue('number')
print '''
<html>
<body>
<h3>Web Based Python script to Convert Fahreheit to Celsius - <a href="http://www.tutorialsmade.com/">Tutorialsmade.com</a></h3>
<form action="" method="post">
<label>Enter Fahreheit <label>
<input type="text" name="number" />
<input type="submit" value="Submit" />
</form><br />
</body>
</html>
'''
if(number):
fahrenheit = float(number)
#convert fahrenheit to celsius
celsius = (fahrenheit - 32)*5/9
print "<p>Celsius:",celsius,"<p>"You can also see the demo of the python web script here:
