0% found this document useful (0 votes)
4 views6 pages

GuidedPractice4 1

gp

Uploaded by

angelineortiz100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

GuidedPractice4 1

gp

Uploaded by

angelineortiz100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Guided Practice 4-1 – 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

Create a file of functions and place them into a functions.py file


Program CallFunctions import functions.py file:
Call each of the functions in the functions file
End.

Deliverables for Task 1

 Pseudocode for functions file

Create a file of functions and place them into a functions.py file


Program CallFunctions import functions.py file:
Call each of the functions in the functions file
End.
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-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.

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’)

It does work, however lines looks a little messy/not super straigth

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

You might also like