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:
1 2 3 4 5 6 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#!/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: