@@ -46,7 +46,6 @@ with commas and giving them different names::
4646 turtle.forward(length)
4747 turtle.backward(length)
4848
49-
5049A parameterized function for a variable size hexagon
5150====================================================
5251
@@ -56,28 +55,49 @@ Exercise
5655Write a function that allows you to draw hexagons of any size you want, each
5756time you call the function.
5857
58+ ::
59+
60+ def hexagon(size):
61+ for _ in range(6):
62+ turtle.forward(size)
63+ turtle.left(60)
64+
5965A function of several parameters
6066================================
6167
6268Exercise
6369--------
6470
65- Write a function that draws a honeycomb with a variable number of hexagons, of
66- variable sizes.
71+ Write a function that will draw a shape of *any * number of sides (let's assume
72+ greater than two) of any side length. Get it to draw some different shapes.
73+
74+ .. tip ::
75+
76+ The sum of the internal angles of any shape is always 360 degrees!
6777
6878Solution
6979--------
7080
7181::
72-
73- def hexagon(size):
74- for i in range(6):
75- turtle.forward(size)
76- turtle.left(60)
77-
78- def honeycomb(size, count):
79- for i in range(count):
80- hexagon(size)
81- turtle.forward(size)
82- turtle.right(60)
8382
83+ def draw_shape(sides, length):
84+ for _ in range(sides):
85+ turtle.forward(length)
86+ turtle.right(360 / sides)
87+
88+ Bonus
89+ -----
90+
91+ It might sound crazy, but it's perfectly possible to pass a *function * as a parameter
92+ to another function! Python regards functions as perfectly normal 'things', the same as
93+ variables, numbers and strings.
94+
95+ For instance, you could create a shape drawing function which turned one way or another
96+ depending on which turtle function you passed to it - ``turtle.left `` or ``turtle.right ``.
97+
98+ See if you can implement this!
99+
100+ .. note ::
101+
102+ Passing a function (e.g ``turtle.left ``) is different than *calling * it, which
103+ would instead be written ``turtle.left(45) ``.
0 commit comments