A good example of how to use functions is a the following modified version of assignment 2.
import sys
#define the variable I will use throughout the program
xcenter = 400
ycenter = 300
scale = 30
#convert an x value from cartesian coordinates to screen coordinates
def cartx(x):
x = scale * x
x = xcenter + x
return x
#convert an x value from cartesian coordinates to screen coordinates
def carty(y):
y = scale * y
y = ycenter - y
return y
#find the y value for the given a,b,c,d and x values
def formula(a,b,c,d,x):
y = a*x**3 + b*x**2 + c*x + d
return y
#Draw the curve defined by a,b,c and d from a beginning cartesian value to an end cartesian value
#approximating by delta cartesian units
def draw(a,b,c,d,begin,end,delta):
x = begin
while x < end:
y = formula(a,b,c,d,x)
y2 = formula(a,b,c,d,x+delta)
print "line",cartx(x),carty(y),cartx(x+delta),carty(y2)
x = x + delta
sys.stdout.flush()
#Draw Axes through the center
print "line",xcenter,0,xcenter,ycenter*2
print "line",0,ycenter,xcenter*2,ycenter
#label X axis
i = 13
while(i > 0):
print "line",cartx(i),carty(0.25),cartx(i),carty(-0.25)
print "line",cartx(-1*i),carty(0.25),cartx(-1*i),carty(-0.25)
print "text",i,cartx(i),carty(-0.75),"label" + str(i),"center"
print "text",-1*i,cartx(-1*i),carty(-0.75),"label" + str(-1*i),"center"
i -= 1
#label Y axis
i = 10
while(i > 0):
print "line",cartx(-.25),carty(i),cartx(.25),carty(i)
print "line",cartx(-.25),carty(-1*i),cartx(.25),carty(-1*i)
print "text",i,cartx(-1),carty(i-0.25)
print "text",-1*i,cartx(-1),carty(-1*i-0.25)
i -= 1
#Assume we want the program to run the first time
answer = "yes"
#While the user wants to draw a curve
while answer == "yes":
#Get a,b,c,d from user
sys.stderr.write("input a:")
a = input() # 0.0
sys.stderr.write("input b:")
b = input() # -0.1
sys.stderr.write("input c:")
c = input() # 0.0
sys.stderr.write("input d:")
d = input() #8.0
#Where do we want our curve to start and end, how much do we want it to go up by
begin = -14
end = 14
delta = 0.1
#Draw Curve
draw(a,b,c,d,begin,end,delta)
#Ask if the user would like to draw another curve
sys.stderr.write("Would you like to draw another? ")
answer = raw_input()
sys.stdout.flush()
In this example, we created a few helper functions, like the ones below to convert values between cartesian values and screen coordinates
#convert an x value from cartesian coordinates to screen coordinates
def cartx(x):
x = scale * x
x = xcenter + x
return x
#convert an x value from cartesian coordinates to screen coordinates
#
def carty(y):
y = scale * y
y = ycenter - y
return y`
The core of the program, that is, the drawing of the curve was also done using functions, as seen here
#Draw the curve defined by a,b,c and d from a beginning cartesian value to an end
cartesian value
#approximating by delta cartesian units
#
def draw(a,b,c,d,begin,end,delta):
x = begin
while x < end:
y = formula(a,b,c,d,x)
y2 = formula(a,b,c,d,x+delta)
print "line",cartx(x),carty(y),cartx(x+delta),carty(y2)
x = x + delta
sys.stdout.flush()`