GuidedPractice4 1
GuidedPractice4 1
In this guided practice we are going to create a number of functions we will add to a library in our
PythonFiles directory. We will then use the import statement to pull all of the functions into a separate
program. In this way you will gather together your functions which you can then use in any Python
programs going forward.
First, create the functions.py library and put it into your PythonFiles folder. The functions file can
include any of the programs from previous weeks but they all need to be defined as functions.
Now create a new file named CallFunctions in your IDLE. Enter the following information:
Import Functions
print(“<Your studentID>”)
Functions.HelloWorld()
Functions.CheckEven()
Functions.Birthday()
Add the functions from your Guided practices for 2-2 (NumberSort.py), 3-1 (ReadNames.py) and 3.2
(ReadWebPage.py and PullPDF.py) to the function file.
Run the new functions from the CallFunctions.py program. Take a screenshot of the program and the
output.
Now we are going to take some of the items we have done in previous labs and put them into their own
functions. This will allow us to call the graphics functions multiple times with only a single command.
Import turtle
def square(size):
for i in range(4):
turtle.forward(size)
turtle.right(90)
square(100)
Run the program to verify that your square function is working properly. Take a screenshot.
Let’s add two more shape functions called circle and rectangle:
def circle(size):
turtle.circle(size)
def rectangle(sideA,sideB):
for i in range(2):
turtle.forward(sideA)
turtle.right(90)
turtle.forward(sideB)
turtle.right(90)
Verify these work by adding the following lines to the end of the program and take a screenshot.
square(100)
turtle.penup()
turtle.forward(150)
turtle.pendown()
circle(50)
turtle.penup()
turtle.forward(50)
turtle.pendown()
rectangle(100,50)
Finally we’re going to add a function that will draw any shape on the turtle screen.
anyshape(5, ‘r’)
Test your function with a different number of sides and take a screenshot of your results.