Below is some code that has been deliberately written in an inefficient way.
The code is supposed to draw 50 smiley faces in random positions using turtle graphics
- Only import the random module once
Hint: modules should only ever be imported once, usually at the top of your code
- Take the definition of the
faceprocedure out of the loop so it’s only defined once
Hint: There is a loop that repeats 50 times to draw 50 faces. Currently, each time the loop repeats it will define the
faceprocedure all over again. Move the procedure definition out of the loop.
- Create a new procedure called
fill_circleto reduce the number of lines
Hint: An important skill in optimising code is spotting repeated sections of code. The sections of code that draw the face and two eyes look very similar. You can replace these sections with a call to
On the next page you’ll get some ideas for projects where you can use functions in your own projects