The snowflake module extends the Turtle class to enable the easy creation of simple snowflakes. This module is built into the Python Adventures (pythonadventures.co.uk) online IDE and is also downloadable for use in other IDEs. The module can be downloaded below. The easiest way to use it is to place the file in the same directory as your program.
Examine the starter code below and predict what you think will happen when the program runs:
import snowflake s = snowflake.Snowflake() def mySnowflake(): s.drawbranch(60) s.drawbranch(60) s.drawbranch(60) s.pencolor("blue") s.goto(-100,100) mySnowflake() s.goto(100,-100) mySnowflake()
You can load the starter program in Python Adventures or download it and open it in an IDE of your choice. Run the program and test your predictions.
Now work through these tasks*:
- Improve the mySnowflake function so it draws all six branches of the snowflake.
- Adapt the program so it draws two more snowflakes in different locations and with different colours.
- There are lots of repeated instructions in the mySnowflake function, use a loop to repeat the instruction the required number of times.
- The drawbranch method has optional parameters which allow us to customise the length, number of arms and arm width. The example below shows how they can be used. Customise your snowflake using these optional parameters.
drawbranch(60, length=50, arms=2, armsize=20)
- Create a new function that draws a snowflake with 8 branches, with custom parameters of your choice. HINT: You will need to change the angle.
- Use your snowflake functions to create your own original design on the canvas.
*these task details can also be accessed by clicking on the ‘i’ button in the Python Adventures version of the activity.