More “Fun” with Funcitons … (Get it?) :)

Introduction

So we’ve been introduced to functions in Python.  Remember that we use a def block to define a function (nothing happens at this point) and then later, we call a function by its name (with parameters) when we want to execute the function.

The following code is a solution to exercise #9 from our tutorial (link).  The program includes a function that draws one five-point star.  Copy the code and verify that it works.  Save the program as yourname-def-star-funtions-9.py.  You do not need to submit the program to me.

# def-star-functions-9.py

import turtle

# ***************************************************************
# Function Definitions
# ***************************************************************

def drawFivePointStar(t):
    """Make turtle t draw a star."""

    for i in range(5):
        t.forward(100)
        t.left(216)

# ***************************************************************
# Main Program Code (Stuff Happens Here)
# ***************************************************************

wn = turtle.Screen()

# Create our turtle
wolfram = turtle.Turtle()

# Draw one star
drawFivePointStar(wolfram)

wn.exitonclick()

Notice in the code that we are using comment blocks with lines of asterisks to divide our code into sections, one for our function definitions and one for our main code block.  This helps improve the readability of the program.

When the Python interpreter begins to execute our program, nothing “happens” until we enter the main section with the command wn = turtle.Screen().

The Assignment

Once you get the code above working, your assignment (Exercise 10 from the tutorial) is to modify the main program code so it draws the picture below (see the hint below the image for a plan):

def-functions-10

 

Hint:  Here’s a plan to complete the project modify the star program with the follow additions:

  1. Use goto() to move your starting point left and up from the center (experiment until it looks right).
  2. Use a for loop to (a) put the pen down, (b) draw a star, (c) pick the pen up, (d) move forward 350 units, and (e) turn right 144 degrees.
  3. Repeat this loop 5 times and you will have it.

Use this color reference link to choose the colors that I have in this image.

A New Way to Submit Your Program to Me

When you have completed this, please save it with the name yourname-def-star-funtions-10.py.   Then, instead of emailing it to me, please submit the file using this submission link.  A page will open with a submission form.  You choose your file from your h: drive and then click submit.  If all goes well you will see a message with the details of the file you uploaded.

Repeat: CLICK THIS LINK TO SUBMIT YOUR PROGRAM 🙂