Skip to content

Commit 006ef4e

Browse files
committed
Rewrite multiple parameter exercise
Instead of a variable hexagon-lattice, write any "any shape" function. Also introduce a bonus exercise. Fixes OpenTechSchool#29
1 parent d780260 commit 006ef4e

1 file changed

Lines changed: 34 additions & 14 deletions

File tree

‎source/functions_parameters.rst‎

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ with commas and giving them different names::
4646
turtle.forward(length)
4747
turtle.backward(length)
4848

49-
5049
A parameterized function for a variable size hexagon
5150
====================================================
5251

@@ -56,28 +55,49 @@ Exercise
5655
Write a function that allows you to draw hexagons of any size you want, each
5756
time 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+
5965
A function of several parameters
6066
================================
6167

6268
Exercise
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

6878
Solution
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

Comments
 (0)