Turtle Challenge Seven
Open tu6 and File, Save As, tu7
Function Concept
Functions can be used many times. When we use a function, we say we call it. Functions are written at the top of the programme. Some functions can have parameters that change how the function is run.
Vocabulary function a section of code that can be called/run many times. Starts with def in Python.
import turtle
import random
t=turtle.Turtle()
wn=turtle.Screen()
#functions
def eqi_triangle_simple(): #Simple example that has no parameters
for i in range(3):
t.fd(50)
t.rt(120)
def eqi_triangle(length, num_sides): #Complex function with two parameter, length and num_sides
for i in range(num_sides):
t.fd(length)
t.rt(360/num_sides)
#Call Functions
eqi_triangle_simple() #Calls the simple example function above
eqi_triangle(30, 3) #30 parameter will go into length and 3 parameter will go into num_sides
t.fd(50)
eqi_triangle(35, 3)
wn.exitonclick()
Predict
1, How many triangles will the code create?
Run
Now run the code
Investigate
1, Explain how this code t.rt(360/num_sides) calculates the angle
2,What does for i in range(3): do?
Screenshot the investigate questions (answers underneath)
Modify
1, Change eqi_triangle_simple so that it draws a square.
List the things you changed A, B, C,
2, Change the code to create an, eqi_triangle whose side is 120.
What did you change?
Screenshot your modify answers
Make
1, Create a couple of functions for shapes other than a triangle. Can you create one with and one without parameters?
2, Use your functions to create a repetitive pattern?
Screenshot your make creations
DEBUG your code if it doesn't work