Saturday, May 4, 2013

Python: Fahrenheit - Celsius Converter Tutorial

Python: Fahrenheit - Celsius Converter Tutorial


Introduction

Hello everybody, this is jimkokko5 on another tutorial. This time, we're talking about how to make a Fahrenheit - Celsius Converter in Python. I was, let's say, inspired to do this by the guys who teach me Interactive Python over at Coursera.org. It involves little knowledge of Python; it's real easy to make. Let's get started!


Getting Started

The first thing we need to do is to find the formulas needed to convert degrees in Fahrenheit to degrees in Celsius and vice versa. The next thing we need to do is to declare some global variables; we will need them later on.

# c = 5 / 9 * (f-32)
# f = 9 / 5 * c + 32

# define global variables
x = 0
y = 0

Making The Initialization Function

Next, what we need to do is set up our initialization function. This function simply displays text, and we need it to run immediately when the program starts.

# initialization function
def init():
    print "==========================================="
    print "Celsius - Fahrenheit Converter, v1.0"
    print "~Made by jimkokko5~"
    print "==========================================="
    print ""

Defining The Event Handlers

What we need to do now is to define our event handlers. These will run when the user enters a value, for example degrees in Celsius to be converted to degrees in Fahrenheit. The first one converts degrees Fahrenheit to degrees in Celsius, and the second one does the opposite.

# define event handlers
def f2c(f):
    f = float(f)
    c = 5 / 9  * (f-32)
    print f, "degrees Fahrenheit is", c, "degrees Celsius"
    print ""
 
def c2f(c):
    c = float(c)
    f = 9 / 5  * c + 32
    print c, "degrees Celsius is", f, "degrees Fahrenheit"
    print ""

As you can see, we take the string the user has entered, we convert it into a float (something close enough to a rational number) and we use the formulas we found earlier to convert it to the opposite value (if it's Celsius convert it to Fahrenheit and vice versa). Finally, we print the results.


Registering The Event Handlers

Last but not least, we need to register our event handlers, aka getting everything to run. This is real easy to do; we will also use our "x" and "y" global variables to hold the input.

# register event handlers
init()

x = input("Convert Fahrenheit to Celsius: ")
y = input("Convert Celsius to Fahrenheit: ")

f2c(x)
c2f(y)

And that's it. Our program is now ready to run!


Full Code

# c = 5 / 9 * (f-32)
# f = 9 / 5 * c + 32

# define global variables
x = 0
y = 0

# initialization function
def init():
    print "==========================================="
    print "Celsius - Fahrenheit Converter, v1.0"
    print "~Made by jimkokko5~"
    print "==========================================="
    print ""
 
# define event handlers
def f2c(f):
    f = float(f)
    c = 5 / 9  * (f-32)
    print f, "degrees Fahrenheit is", c, "degrees Celsius"
    print ""
 
def c2f(c):
    c = float(c)
    f = 9 / 5  * c + 32
    print c, "degrees Celsius is", f, "degrees Fahrenheit"
    print ""
 
# register event handlers
init()

x = input("Convert Fahrenheit to Celsius: ")
y = input("Convert Celsius to Fahrenheit: ")

f2c(x)
c2f(y)

Full Code (Python 3.0)

# c = 5 / 9 * (f-32)
# f = 9 / 5 * c + 32

# define global variables
x = 0
y = 0

# initialization function
def init():
    print ("===========================================")
    print ("Celsius - Fahrenheit Converter, v1.0")
    print ("~Made by jimkokko5~")
    print ("===========================================")
    print ("")
 
# define event handlers
def f2c(f):
    f = float(f)
    c = 5 / 9  * (f-32)
    print (f, "degrees Fahrenheit is", c, "degrees Celsius")
    print ("")
 
def c2f(c):
    c = float(c)
    f = 9 / 5  * c + 32
    print (c, "degrees Celsius is", f, "degrees Fahrenheit")
    print ("")
 
# register event handlers
init()

x = input("Convert Fahrenheit to Celsius: ")
y = input("Convert Celsius to Fahrenheit: ")

f2c(x)
c2f(y)

No comments:

Post a Comment