Turtle Functions Project
When it is time to take the quiz, the Quiz link will be written on the board. Enter the quiz code in the text field below and click "Submit".
Important: There is a problem with how some Micro:bit functions
are displayed on the quiz page. The texts
microbit.display.show, accelerometer.is_, and
display.is_ are being displayed as hyperlinks. Ignore the fact
that they are blue and underlined, and do not click on those
links, as they will take you to nonexistent or fake websites.
Define your own drawing commands.
Look at the following code example. It includes a definition for a
function called square() that draws a square of a particular
size. Notice that this function does not return a value. This is something
that is common in Python when you need a function that performs an action
but does not need to give back a value. In this case, the function performs
the action of drawing a square, but it does not return any information to
the code that uses the function. The print() function is an
example of a function that does not return a value that we use all the time.
The turtle graphics functions that we have been using also do not return values. They
just perform actions that change the state of the turtle and the drawing.
Once the function is defined, I have a new turtle graphics command that draws a square. I can then use it in my program in the same way that I use the other turtle graphics commands that are in the turtle module.
from turtle import * def square(size): pendown() for i in range(4): forward(size) right(90) penup() speed(0) pencolor("green") pensize(2) s = 10 for j in range(0, 24): square(s) forward(s * 2) left(30) s += 3
Here are some definitions for drawing a triangle or hexagon that are
similar to the square function but with changes to the number of sides and
the angles. Study them to understand how they are different from
square() and from each other.
def triangle(size): pendown() for i in range(3): forward(size) right(120) penup() def hexagon(size): pendown() for i in range(6): forward(size) right(60) penup()
If you understand how those functions work, see if you can figure out this next one, which draws a polygon of any number of sides. It has a second parameter that tells the function how many sides to draw.
def polygon(size, sides): pendown() for i in range(sides): forward(size) right(360 / sides) penup()
Due at the end of class on Monday, April 13.
Write a program that uses any or all of the functions shown above. Put the function definitions you want to use near the top of your program, after the imports. Then put your main program, which uses the functions you defined. Try using different colors and different loops.
Bonus: Try defining your own functions to draw different things like a star or a right triangle.
Save your program as lastname_turtle_fun.py and
upload it to your OneDrive folder. It is due at the end of class on
Monday, April 13.