Test Review
Reading Questions
Quiz
Writing Functions
- We know how to use functions…
print
,turtle.forward
,math.cos
- Functions are named code, they do something
We can define our own named code
def draw_something(): turtle.forward(100) turlte.left(90)
- Execute the function just like other functions,
draw_something()
Note, order matters, you can’t use it until you have defined it, just like a variable
Parameters and Arguments
- Note that the previous example creates a new turtle every time it is executed
- We can fix this if by having the function share the same turtle across multiple executions.
- To do this we need to give the function input, the turtle to use
Parameters are variables with a value set outside of the function
def draw_something(dist): turtle.forward(dist) turtle.left(90)
Now, it can be called like this
draw_something(10)
- This is much like the turtle functions we have already used…
turtle.forward(100)
, the 100 is the input to the function, it changes the behavior of the function - The names of the inputs, in the function definition, are called parameters
- The names of the inputs, in the function call, are called arguments
If a function has multiple parameters, then the order of the arguments is the same
def draw_something(dist, angle): turtle.forward(dist) turtle.left(angle) draw_something(10, 20)
In this case,
dist
has the value 10 andangle
has the value 20 because the parameterdist
is before the parameterangle
Drawing Images
- For the lab today you will need to draw images using the grahpics module
- The graphics module can only draw gif images
- To find a gif do a google image search and
filetype:gif
to your search terms - Put the image in the same directory and you .py file and the graphic.py file
Draw an image with the
draw_image
functiongraphics.draw_image('ball.gif', 0, 0)
- The first argument is the name of the image file, don’t for get the quotes
The second and third arguments are the x and y coordinates of the center of the image