Skip to content

Commit a7dcafc

Browse files
committed
Add content for conditionals
1 parent 8d30351 commit a7dcafc

File tree

2 files changed

+66
-9
lines changed

2 files changed

+66
-9
lines changed

docs/en/conditionals.rst

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ Introduction
55

66
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*.
77

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*: *=,>,<*.
1215

1316

1417
Examples
@@ -30,10 +33,64 @@ Here are some simple examples::
3033
forward (-direction)
3134

3235

36+
Condition an action
37+
===================
38+
3339
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)
74+
75+
Solution
76+
--------
77+
78+
::
79+
80+
def uturn_at_border(distance):
81+
if heading() == 0 :
82+
future_x_coord = xcor() + distance
83+
else:
84+
future_x_coord = xcor() - distance
85+
86+
diff = window_width()/2-future_x_coord
87+
if diff > 0 :
88+
forward(distance)
89+
else:
90+
forward(window_width()/2-xcor())
91+
left(180)
92+
forward (-diff)
93+
94+
3595

36-
[some example...]
3796

38-
Bonus Exercise
39-
==============

docs/en/functions_parameters.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,4 @@ Solution
6969
hexagon(size)
7070
forward(size)
7171
right(60)
72-
72+

0 commit comments

Comments
 (0)