Skip to content

Commit 62f2fb7

Browse files
committed
Rewrite problems, expand wording
Fixes OpenTechSchool#41 loops now introduced properly before functions
1 parent 3168d5f commit 62f2fb7

3 files changed

Lines changed: 71 additions & 94 deletions

File tree

‎source/functions.rst‎

Lines changed: 35 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@ User-defined functions
44
Introduction
55
============
66

7-
There is still a lot of duplicated code --- the actual drawing of the rectangle
8-
--- around. If you need to copy and paste code, that is usually a sign of
9-
lacking abstractions. (Programmers call it a *code smell.*)
7+
Programmers can deal with some pretty complex and abstract problems, but one
8+
sign of a good programmer is that they're lazy. They only like to deal with one
9+
thing at a time. So you need a way to break up code into smaller, discrete
10+
pieces, which lets you focus on just the piece you want to.
1011

11-
Functions are one way to express abstractions in Python. Let's take
12-
``turtle.reset()`` for example. It is actually an abstraction for a number of
13-
steps, namely:
12+
Functions are one way to express such abstractions in Python. Let's take
13+
``turtle.reset()`` for example. ``reset`` is a function we call on our ``turtle``, and
14+
it is actually an abstraction for a number of steps, namely:
1415

1516
* Erase the drawing board.
1617
* Set the width and color back to default.
1718
* Move the turtle back to its initial position.
1819

20+
But because all the code is contained in the function, we don't have to worry about these
21+
details. We can simply *call* this function, and know it will do what it says for us.
22+
23+
So - how to write your own?
24+
1925
A function can be defined with the ``def`` keyword in Python::
2026

2127
def line_without_moving():
@@ -43,11 +49,7 @@ We could write more functions to remove some of the repetition::
4349
line_without_moving()
4450
turtle.right(360 / 5)
4551

46-
def star():
47-
star_arm()
48-
star_arm()
49-
star_arm()
50-
star_arm()
52+
for _ in range(5):
5153
star_arm()
5254

5355
.. important::
@@ -87,14 +89,9 @@ Solution
8789

8890
def tilted_square():
8991
turtle.left(20) # now we can change the angle only here
90-
turtle.forward(50)
91-
turtle.left(90)
92-
turtle.forward(50)
93-
turtle.left(90)
94-
turtle.forward(50)
95-
turtle.left(90)
96-
turtle.forward(50)
97-
turtle.left(90)
92+
for _ in range(4):
93+
turtle.forward(50)
94+
turtle.left(90)
9895

9996
tilted_square()
10097
tilted_square()
@@ -104,35 +101,15 @@ Solution
104101
# which might be useful later:
105102

106103
def square():
107-
turtle.forward(50)
108-
turtle.left(90)
109-
turtle.forward(50)
110-
turtle.left(90)
111-
turtle.forward(50)
112-
turtle.left(90)
113-
turtle.forward(50)
114-
turtle.left(90)
104+
for _ in range(4):
105+
turtle.forward(50)
106+
turtle.left(90)
115107

116108
def tilted_square():
117109
turtle.left(20)
118110
square()
119111

120-
# etc'
121-
122-
Comments
123-
--------
124-
125-
In the solution above, the line that starts with a ``#`` is called a
126-
comment. In Python, anything that goes on a line after ``#`` is ignored
127-
by the computer. Use comments to explain what your program does,
128-
without changing the behaviour for the computer.
129-
130-
Comments can also go at the end of a line, like this:
131-
132-
::
133-
134-
turtle.left(20) # now we can change the angle only here
135-
112+
# etc
136113

137114
A function for a hexagon
138115
========================
@@ -148,48 +125,28 @@ Now combine that function into a honeycomb. Just make it with a single layer lik
148125

149126
.. image:: /images/honeycomb.png
150127

128+
Give it a good go!
129+
130+
.. hint::
131+
132+
Make sure your hexagon function returns your turtle to exactly the same
133+
position and angle it was before it was asked to draw the hexagon. This
134+
makes it easier to reason about.
135+
151136
Solution
152137
--------
153138

154139
::
155140

156141
def hexagon():
157-
turtle.forward(100)
158-
turtle.left(60)
159-
turtle.forward(100)
160-
turtle.left(60)
161-
turtle.forward(100)
162-
turtle.left(60)
163-
turtle.forward(100)
164-
turtle.left(60)
165-
turtle.forward(100)
166-
turtle.left(60)
167-
turtle.forward(100)
168-
turtle.left(60)
169-
170-
hexagon()
171-
turtle.forward(100)
172-
turtle.right(60)
173-
174-
hexagon()
175-
turtle.forward(100)
176-
turtle.right(60)
177-
178-
hexagon()
179-
turtle.forward(100)
180-
turtle.right(60)
142+
for _ in range(6):
143+
turtle.forward(100)
144+
turtle.left(60)
181145

182-
hexagon()
183-
turtle.forward(100)
184-
turtle.right(60)
185-
186-
hexagon()
187-
turtle.forward(100)
188-
turtle.right(60)
189-
190-
hexagon()
191-
turtle.forward(100)
192-
turtle.right(60)
146+
for _ in range (6):
147+
hexagon()
148+
turtle.forward(100)
149+
turtle.right(60)
193150

194151
You could also put the ``turtle.forward(100); turtle.right(60)`` portion in the
195152
function, but you better not call it ``hexagon`` in that case. That's

‎source/functions_parameters.rst‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ As we shrink down our code and add functions to remove duplication, we
88
are *factorizing* it. This is a good thing to do. But the functions we
99
have defined so far are not very flexible. The variables are defined
1010
inside the function, so if we want to use a different angle or a
11-
distance then we need to write a new function.
11+
distance then we need to write a new function. Our hexagon function can
12+
only draw one size of hexagon!
1213

1314
That is why we need to be able to give parameters, also called
1415
*arguments*, to the function. This way the *variables* in the

‎source/loops.rst‎

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Introduction
66

77
One more thing: Our programs often feature repetition. There is a powerful
88
concept in Python called looping (jargon: *iteration*), which we will
9-
elaborate later on. For now, try this easy example::
9+
elaborate later on. For now, **try this easy example**::
1010

1111
for i in range(10):
1212
print("Hello!")
@@ -79,32 +79,51 @@ Can you make the dashes become larger as the line progresses?
7979
Can you utilize ``i`` --- commonly called the **index** variable or loop
8080
variable --- to get increasing step sizes?
8181

82-
Honeycomb loops
83-
===============
82+
Comments
83+
--------
84+
85+
In the solution above, the line that starts with a ``#`` is called a
86+
comment. In Python, anything that goes on a line after ``#`` is ignored
87+
by the computer. Use comments to explain what your program does,
88+
without changing the behaviour for the computer.
89+
90+
Comments can also go at the end of a line, like this:
91+
92+
::
93+
94+
turtle.left(20) # now we can change the angle only here
95+
96+
More Efficient Squares
97+
======================
8498

8599
Exercise
86100
--------
87101

88-
Take your honeycomb program and make it easier with loops. How small can you
89-
get it?
102+
The squares we were drawing at the start of this tutorial had a lot of
103+
repeated lines of code. Can you write out a square drawing program in fewer
104+
lines by utilizing loops?
90105

91106
Solution
92107
--------
93108

94109
::
95110

96-
def hexagon():
97-
for i in range(6):
98-
turtle.forward(100)
99-
turtle.left(60)
100-
101-
for i in range(6):
102-
hexagon()
111+
for _ in range(4):
103112
turtle.forward(100)
104-
turtle.right(60)
113+
turtle.left(90)
105114

106115
Bonus
107116
-----
108117

109-
See if you can look at your earlier square-drawing code, and make it much
110-
shorter by using loops.
118+
Try *nesting* loops, by putting one right under (*inside*) the other, with some
119+
drawing code that's inside both. Here's what it can look like::
120+
121+
for ...:
122+
for ...:
123+
# drawing code inside the inner loop goes here
124+
...
125+
# you can put some code here to move
126+
# around after
127+
...
128+
129+
Replace the ``...`` with your own stuff!

0 commit comments

Comments
 (0)