Debug it
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
Challenges
- 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
face
procedure 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
face
procedure all over again. Move the procedure definition out of the loop. - Create a new procedure called
fill_circle
to 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
fill_circle
On the next page you’ll get some ideas for projects where you can use functions in your own projects
Page 1: Intro
Page 2: The theory: learn what you need to know as fast as possible.
Page 3: Try it: try out and adapt some working python code snippets.
Page 4: Debug it: Learn how to find and fix common mistakes.
Page 5: Extend it: Choose a project idea to use your newfound python skills.