Making a Function

While it is interesting to control the turtle in this manner, it would be tedious to make anything complex, and even more so if you wanted to change what you draw. So what we’ll do next is create some of our own functions. The first function we’ll make is a “jump” function, which will allow our turtle to hop forward without drawing anything.

To create a new function, use the keyword “def”, followed by the function name “jump”, and set of parenthesis followed by a colon. Leave the parenthesis empty for now.

def jump():

Now that we have our jump function defined, we will add in the things we want our turtle to do. For a jump, the turtle is first going to lift the pen, so it doesn’t draw as it moves, move forward, and then lower the pen so it can draw again. Bear in mind that all of these lines should be indented, so that python knows that these lines of code are a part of your “jump” function

def jump():
    turtley.penup()
    turtley.forward(30)
    turtley.pendown()

Now that we have the function defined, we can call it alongside our other statements to make the turtle move. Give it a try, an example of a drawing you may make is on the right. Note that jump is actually a function we defined, and not a function of the Turtle, so we only put jump() and not turtley.jump()

turtley.forward(20)
jump()
turtley.forward(20)