Skip to content

Commit b53c668

Browse files
nekopanicprojectgus
authored andcommitted
refresh
1 parent c370b87 commit b53c668

18 files changed

+230
-163
lines changed

en/_sources/conditional_loops.txt

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,46 @@ conditional loops are defined with the ``while`` statement::
1111
counter = 0
1212
while counter < 10:
1313
print("in the loop")
14-
counter += 1
15-
16-
The `+=' operator is an inplace increment and it is a short way of saying `counter = counter + 1` in the above statement.
14+
counter = counter + 1
1715

18-
Move until condition is met
19-
===========================
16+
17+
Turtle prison
18+
=============
2019

2120
Exercise
2221
--------
2322

24-
Use the ``while`` loop to move the turtle forward until it reaches the end of
25-
the window.
23+
The turtle has been up to its usual tricks again, robbing liqour
24+
stores and building up huge gambling debts. It's time for turtle to be
25+
put into a box that it can't get out of.
26+
27+
Let's make a new version of ``forward()``. One that turns the turtle
28+
around if it tries to go further than 100 from the origin. We'll need
29+
a ``while`` loop, and some new turtle functions:
30+
31+
* ``turtle.distance(0,0)`` - Distance of the turtle from the origin
32+
* ``turtle.towards(0,0)`` - The angle to get back to origin
33+
* ``turtle.setheading(angle)`` - Directly set the turtle direction
34+
35+
Now you will need to implement the prison logic using the API calls
36+
above, a ``while`` loop and a bit of conditional logic. It's a bit of
37+
a stretch but keep at it! Don't be afraid to talk it out with a coach
38+
or another student.
39+
2640

2741
Solution
2842
--------
2943

3044
::
3145

32-
def move_to_window_border(distance):
33-
while turtle.xcor() + distance < turtle.window_width() / 2:
34-
turtle.forward(distance)
46+
def forward(distance):
47+
while distance > 0:
48+
if turtle.distance(0,0) > 100:
49+
angle = turtle.towards(0,0)
50+
turtle.setheading(angle)
51+
turtle.forward(1)
52+
distance = distance - 1
3553

36-
Bonus
37-
-----
38-
39-
Can you make the turtle move exactly to the window border?
4054

4155
Draw a spiral
4256
=============
@@ -53,23 +67,29 @@ Interrupt the loop when the turtle reaches a certain distance from the center.
5367
Use the function ``turtle.distance(x, y)`` to get the turtle's distance to the
5468
point defined by the coordinates ``x`` and ``y``.
5569

70+
To do this you will need the ``turtle.xcor()`` and ``turtle.ycor()``
71+
functions, which return the position of the turtle in X and Y axes
72+
respectively.
73+
5674
.. note::
5775

5876
To draw a spiral, the turtle has to rotate by a constant value and move
59-
forward by an increasing value in each repetition.
77+
forward by an increasing value.
6078

6179
Solution
6280
--------
6381

6482
::
6583

6684
def draw_spiral(radius):
85+
original_xcor = turtle.xcor()
86+
original_ycor = turtle.ycor()
6787
speed = 1
6888
while True:
6989
turtle.forward(speed)
7090
turtle.left(10)
7191
speed += 0.1
72-
if turtle.distance(0, 0) > radius:
92+
if turtle.distance(original_xcor, original_ycor) > radius:
7393
break
7494

7595
Bonus

en/_sources/conditionals.txt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ Conditional statements
44
Introduction
55
============
66

7-
So far we have accomplished predefined tasks, but in all honesty we were accomplishing no better achievements than the 18th century automata, or the music boxes following one set of instruction to the end. What makes programming so much more powerful are conditional statements. This is the ability to *test* a variable against a value and act in one way if the condition is met by the variable or an other way if not. They are also commonly called by programmers *if statements*.
7+
So far we have accomplished predefined tasks, but in all honesty we
8+
were accomplishing no better than old-time music boxes following one
9+
set of instructions to the end. What makes programming so much more
10+
powerful are conditional statements. This is the ability to *test* a
11+
variable against a value and act in one way if the condition is met by
12+
the variable or another way if not. They are also commonly called by
13+
programmers *if statements*.
814

915
To know if a condition is *True* of *False*, we need a new type of data:
1016
the booleans. They allow logical operations.
@@ -20,8 +26,8 @@ The condition can be anything that evaluates as *True* or
2026
*False*. Comparisons always return *True* or *False*, for example
2127
``==`` (equal to), ``>`` (greater than), ``<`` (less than.)
2228

23-
The second **else** part is optional. If you leave it off, nothing
24-
will happen if the conditional evaluates to 'False'.
29+
The **else** part is optional. If you leave it off, nothing will
30+
happen if the conditional evaluates to 'False'.
2531

2632

2733
Examples
@@ -48,14 +54,18 @@ Giving Directions
4854
=================
4955

5056
Python turtles can be very good at following instructions. Let's use
51-
the ``raw_input()`` function to ask the user for a direction to move
57+
the ``input()`` function to ask the user for a direction to move
5258
the turtle. To keep things easy we will only accept two instructions:
5359
"left" and "right".
5460

61+
.. note::
62+
63+
Using Python 2? The ``input()`` function is called ``raw_input()``.
64+
5565
It's much easier to define this as a function, like so::
5666

5767
def move():
58-
direction = raw_input("Go left or right? ")
68+
direction = input("Go left or right? ")
5969
if direction == "left":
6070
turtle.left(60)
6171
turtle.forward(50)

en/_sources/functions.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ You can access variables in functions as well::
3535

3636
.. important::
3737

38-
Python uses *identing with whitespace to identify blocks of code*
38+
Python uses *identing with whitespace* to identify blocks of code
3939
that belong together. In Python a block (like the function
4040
definitions shown above) is introduced with a colon at the end of the
4141
line and subsequent commands are indented --- usually 4 spaces
4242
further in. The block ends with the first line that isn't indented.
4343

4444
This is different to many other programming languages, which use
45-
special characters (like curly brackets ``{}``) to group blocks of
45+
special characters (like curly braces ``{}``) to group blocks of
4646
code together.
4747

4848
You can indent your blocks with either tabs or spaces, but if you
@@ -66,7 +66,6 @@ Solution
6666

6767
def tilted_square():
6868
turtle.left(angle)
69-
7069
turtle.forward(50)
7170
turtle.left(90)
7271
turtle.forward(50)
@@ -144,4 +143,4 @@ function, but you better not call it ``hexagon`` in that case. That's
144143
misleading because it actually draws a hexagon and then advances to a position
145144
where another hexagon would make sense in order to draw a honeycomb. If you
146145
ever wanted to reuse your hexagon function outside of honeycombs, that would be
147-
confusing at least.
146+
confusing.

en/_sources/getting_started.txt

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ Python similar to this::
1717

1818
.. note::
1919

20-
If you're on Windows and instead see something like "command not found," the
21-
installer has not set up your *search path* correctly, ie. Windows does not
22-
know where to find :program:`python.exe`. Follow the steps described in the
23-
`Python docs`__ and **restart your command line.**
24-
25-
__ http://docs.python.org/using/windows.html#excursus-setting-environment-variables
20+
On Windows you can open Python through the Start Menu.
2621

2722
Those three ``>>>`` in the last line indicate that you are now in the
2823
interactive shell of Python. It is waiting for your commands::
@@ -43,14 +38,15 @@ Running Python files
4338
====================
4439

4540
But you don't want to type everything into the Python shell every time.
46-
Instead having a file with commands and handing that to Python to execute would
47-
be much better. In order to do that you can just pass a file name to the
48-
:program:`python` executable and it will execute that file instead of launching
41+
Instead you can save the commands to a file and pass a file name to the
42+
:program:`python` executable. It will execute that file instead of launching
4943
the interactive interpreter.
5044

5145
Let's try that. Create a file :file:`hello.py` in your current directory with
5246
your favorite text editor and paste the print command from above. Now save
53-
that file, go back the command line and type:
47+
that file.
48+
49+
On Mac OSX and Linux go back the command line and type:
5450

5551
.. code-block:: bash
5652

@@ -67,21 +63,17 @@ that file, go back the command line and type:
6763

6864
This changes to the subdirectory Python_Exercises of the currently active
6965
directory. If you don't know which directory your shell is currently
70-
running in use :command:`pwd` on Linux or :command:`cd` without parameters
71-
on Windows.
66+
running in use :command:`pwd`.
7267

73-
If you need to change your drive on Windows, just type the drive's letter
74-
followed by a colon::
75-
76-
C:
68+
On Windows you can double-click the Python file to run it.
7769

7870
When pressing :kbd:`<Enter>` now, the file is executed and you see the output
7971
as before. But this time, after Python finished executing all commands from
8072
that file it exits instead of going back to the interactive shell.
8173

8274
.. tip::
8375

84-
Wordpad, TextEdit, Notepad, and Word are **not** suited text editors. If
76+
Wordpad, TextEdit, Notepad, and Word are **not** suitable text editors. If
8577
you are unsure whether you already have a usable editor, you might want to
8678
download and install `Sublime Text <http://www.sublimetext.com/>`_.
8779
Sophisticated editors like this also take care of identation and help you
@@ -95,3 +87,4 @@ And now we are all set and can get started with turtle!
9587
--- rather use more appropriate names such as :file:`square.py` or
9688
:file:`rectangle.py`. Otherwise, whenever you refer to ``turtle``, Python
9789
will pick up *your* file instead of the standard turtle.
90+

en/_sources/logical_operators.txt

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ them. This can be done with logical operators.
1313
Negation of a statement
1414
=======================
1515

16-
We can take the opposite value of a statement to use ``not`` its value.
17-
It is a logical operator::
16+
If we want something to be *False* we can use ``not``. It is a logical
17+
operator::
1818

1919
x = False
2020
if not x :
@@ -46,8 +46,7 @@ This and that or something else
4646
===============================
4747

4848
Two easy to understand operators are ``and`` and ``or``. They do exactly what
49-
they sound like: combine two statements in a way both have be *True* (``and``)
50-
or at least one of them has to be *True* (``or``)::
49+
they sound like:::
5150

5251
if 1 < 2 and 4 > 2:
5352
print("condition met")
@@ -64,25 +63,21 @@ want.
6463
Exercise
6564
--------
6665

67-
Create a function that accepts the argument ``angle`` and moves the turtle
68-
forward into that direction until either the vertical or horizontal distance to
69-
the center exceeds 100.
66+
Earlier we put the turtle in a circular prison. This time let's make
67+
it a box. If the turtle goes more than 100 in the X *or* Y axis then
68+
we turn the turtle back around to the center.
7069

7170
Solution
7271
--------
7372

7473
::
7574

76-
def move_to_square_border(angle):
77-
turtle.left(angle)
78-
x = turtle.xcor()
79-
y = turtle.ycor()
80-
while x > -100 and x < 100 and y > -100 and y < 100:
81-
turtle.forward(10)
82-
x = turtle.xcor()
83-
y = turtle.ycor()
84-
85-
Bonus
86-
-----
87-
88-
Can you use this to draw a star?
75+
def forward(distance):
76+
while distance > 0:
77+
if (turtle.xcor() > 100
78+
or turtle.xcor() < -100
79+
or turtle.ycor() > 100
80+
or turtle.ycor() < -100):
81+
turtle.setheading(turtle.towards(0,0))
82+
turtle.forward(1)
83+
distance = distance - 1

en/_sources/loops.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ once. But it gets better::
2222
Notice how we write only one line of code using ``i`` but it has 10 different
2323
values?
2424

25-
You can also loop over elements of your choice (mathematicians call this
26-
"extensional sets")::
25+
You can also loop over elements of your choice::
2726

2827
for i in 5, 7, 11, 13:
2928
print(i)
@@ -67,7 +66,7 @@ Can you make the dashes become larger as the line progresses?
6766
Feeling lost? Inspect ``i`` at every run of the loop::
6867

6968
for i in range(10):
70-
print i
69+
print(i)
7170
turtle.forward(15)
7271
turtle.penup()
7372
turtle.forward(5)

en/_sources/simple_drawing.txt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ by the given distance. ``turtle.left(...)`` takes a number of degrees which you
4040
to rotate to the left. (There are ``turtle.backward(...)`` and
4141
``turtle.right(...)``, too.)
4242

43+
The standard turtle is just a triangle. That's no fun! Let's make it a turtle
44+
instead with the ``turtle.shape()`` command::
45+
46+
turtle.shape("turtle")
47+
48+
So much cuter!
49+
4350
If you put the commands into a file, you might have recognized that the turtle
4451
window vanishes after the turtle finished its movement. (That is because
4552
Python exits when your turtle has finished moving. Since the turtle window
@@ -49,6 +56,8 @@ until you click on it::
4956

5057
import turtle
5158

59+
turtle.shape("turtle")
60+
5261
turtle.forward(25)
5362

5463
turtle.exitonclick()
@@ -94,7 +103,9 @@ If you want to get creative, you can modify your shape with the
94103
use these functions? Before you can use a function you need to know
95104
its *signature* (for example the number of parameters and what they
96105
mean.) To find this out you can type ``help(turtle.color)`` into the
97-
Python shell. Press the :kbd:`q` key to exit the help again.
106+
Python shell. If there is a lot of text, Python will put the help text
107+
into a *pager*, which lets you page up and down. Press the :kbd:`q`
108+
key to exit the pager.
98109

99110
Alternatively, browse the `online documentation`__.
100111

@@ -122,7 +133,7 @@ Drawing a rectangle
122133
Exercise
123134
--------
124135

125-
Can you draw a (non-square) rectangle too?
136+
Can you draw a rectangle too?
126137

127138
.. image:: /images/rectangle.png
128139

@@ -143,8 +154,8 @@ Solution
143154
Bonus
144155
-----
145156

146-
How about a triangle? (A triangle with 120 degrees angles will have all sides
147-
equally sized.)
157+
How about a triangle? In an equilateral triangle (a triangle with all
158+
sides of equal length) each corner has an angle of 120 degrees.
148159

149160

150161
More squares
@@ -153,7 +164,7 @@ More squares
153164
Exercise
154165
--------
155166

156-
Now, draw a tilted square. And another one, and another one. You can
167+
Now, draw a tilted square. And another one, and another one. You can
157168
experiment with the angles between the individual squares.
158169

159170
.. image:: /images/tiltedsquares.png

0 commit comments

Comments
 (0)