You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/conditionals.rst
+65-8Lines changed: 65 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,10 +5,13 @@ Introduction
5
5
6
6
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 is conditional statements. This is the ability to *test* a variable (or a name) 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
7
8
-
To know if a condition is *true* of *false*, we need a new type of data:
9
-
the booleans. They allow logic operations. A logic statement or operation
10
-
can be avaluated to be true or false. Our conditional statement can then be
11
-
understood like this: **if** *(evaluation of a condition returns true)* **then** *do some operation* **else** *do an other operation*. And any operation that can be avaluated as *true* or *false* can but put to the test. All comparisons return *true* or *false*: *=,>,<*.
8
+
To know if a condition is *True* of *False*, we need a new type of data:
9
+
the booleans. They allow logical operations.
10
+
A logic statement or operation can be evaluated to be *true* or *false*.
11
+
Our conditional statement can then be understood like this:
12
+
13
+
**if** *(evaluation of a condition returns true)* **then** *do some operation* **else** *do an other operation*.
14
+
And any operation that can be avaluated as *true* or *false* can but put to the test. All comparisons return *true* or *false*: *=,>,<*.
12
15
13
16
14
17
Examples
@@ -30,10 +33,64 @@ Here are some simple examples::
30
33
forward (-direction)
31
34
32
35
36
+
Condition an action
37
+
===================
38
+
33
39
Exercise
34
-
========
40
+
--------
41
+
42
+
Let's try to make our turtle to stop when it reaches the end
43
+
of the window. Let's only take into account horizontal movements.
44
+
We want the turtle to go forward for a specified distance (taken as a function parameter) until it reaches this distance, or stop and turn around if it reaches the border of the screen.
45
+
46
+
.. note::
47
+
* We start from the center of the screen, which has coordinate (0;0)
48
+
* We won't turn, until we reach the boundery our turtle wants to get away as quickly as possible
49
+
* We can get the x coordinate of our turtle with the function *xcor*
50
+
* We can get the width of the screen with the function *window_width*
51
+
52
+
Solution
53
+
--------
54
+
55
+
::
56
+
57
+
def stop_at_end_of_screen(distance):
58
+
future_x_coord = xcor() + distance
59
+
diff = window_width()/2-future_x_coord
60
+
if diff > 0 :
61
+
forward(distance)
62
+
else:
63
+
forward(window_width()/2-xcor())
64
+
left(180)
65
+
66
+
Bonus exercise
67
+
--------------
68
+
69
+
Now when the turtle reaches the end of the screen, it turns around and
70
+
continue its movement in the other direction, the remaining of the distance
71
+
72
+
.. note::
73
+
* heading() gives you the current angle the turtle's direction makes with the original east facing turtle. (a turtle going straight up has a heading of 90 degrees)
0 commit comments