Python scripting
Guided Practice 4.2 – Creating and Calling a library of functions
Task 1 – Make a pseudocode – Creating and Calling library functions
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.
We will now create the pseudocode for the program
Create the pseudocode for a program that will
· Create a file of functions and place them into a functions.py file
· Make a new program called CallFunctions and import the functions.py file
· Call each of the functions in the functions file
Deliverables for Task 1
· Pseudocode for functions file
Task 2 – Write and test the function to import functions
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()
Run the program and verify it is working properly.
Add the functions from your Guided practices for 2.3 (NumberSort.py), 3.2 (ReadNames.py) and 3.3 (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.
Deliverables for Task 2
· Screenshot of the your code and the output of your program.
Task 3 – Simple Turtle graphics in a function
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.
Let’s put in the function for a square, program the following:
import turtle
def square(size):
for i in range(4):
turtle.forward(size)
turtle.right(90)
Test your function by putting at the bottom:
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.
Add the following to your program
def anyshape(sides, direction):
angle = int(360/sides)
size = int(900/sides)
for I in range(sides):
turtle.forward(size)
if direction == ‘r’:
turtle.right(angle)
else:
turtle.left(angle)
You can also test the function by typing
anyshape(5, ‘r’)
Try using the number 7. Why doesn’t it work?
anyshape(7, ‘r’)
Test your function with a different number of sides and take a screenshot of your results.
Deliverables for Task 3
Screenshot of your square
Screenshot of your square, circle, and rectangle
Answer question about anyshape with 7 sides.
Screenshot of your anyshape function