Better Functions
Table of Contents
Grey overlay
Pink
Green
Blue
Cream
Liliac
Purple
Yellow

1 Objectives
Developing the Programming and Development and Algorithm learning strands, specifically:
- further developing understanding of repetitions (loops) in programming and being able to use for loops in creating patterns.
- further developing understanding of functions in programming and being able to create simple functions that serve a single purpose.
- developing understanding of the need for data structure list and being able to apply list data structure.
2 Lists
Learn It
- A standard variable is handy for storing a single piece of information.
yourName='Jane' yourAge=50 likeCrisps=True
- Sometimes, it's handy to store several pieces of information so that we can access them all when we need them. For this, we use a list.
- Type these commands in the INTERPRETER one at a time to get the hang of manipulating lists:
someColours=['Red','Blue','Pink','Black'] print(someColours[0]) someColours.append('Green') print(someColours) someColours.sort() print(someColours) someColours.reverse() print(someColours) someColours.pop() print(someColours) someColours.pop(1) print(someColours)
- You can type the commands into this Trinket, if you find it easier…
Code It
- Using lists, we can get more flexibility over the patterns we can create.
import turtle wn = turtle.Screen() wn.bgcolor("white") wn.title("List Practice") tess = turtle.Turtle() tess.pensize(5) def colouredSquare(sideLength,newColour): tess.color(newColour) for x in range(4): tess.forward(sideLength) tess.left(90) colourList=['red','green','blue','orange','hotpink','purple'] for eachColour in colourList: colouredSquare(75,eachColour) # Draw a square in the current colour. # The len() function will tell you how long a list is, or how many characters are in a string. # I'm using it here to calculate the angle I need, based on the size of the list. tess.left(360/len(colourList)) wn.mainloop()
- As always, this Trinket may be handy to use to experiment with…
3 Assessment
Badge It
- Silver: Modify the code to draw the first square with side lengths of 20, the second with size 40, third with 60 and so on.
- Gold: Change the code so that the user can enter five colours that are used to draw a pattern of your choice.
- Platinum: Change the code so that the user can enter as many colours as they like, and have a shape drawn with that many sides. E.g.
Name a Python colour (XYZ when done): red Name a Python colour (XYZ when done): blue Name a Python colour (XYZ when done): green Name a Python colour (XYZ when done): XYZ Here goes...
- Tip: You'll need to use a WHILE loop to make this work.