Skip to content

Commit 3168811

Browse files
committed
Added sections about logical operators and while loops
1 parent 62bd6f4 commit 3168811

File tree

3 files changed

+111
-5
lines changed

3 files changed

+111
-5
lines changed

docs/en/conditional_loops.rst

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,68 @@ Conditional Loops
44
Introduction
55
============
66

7+
Conditional loops are way to repeat something until a certain condition is met.
8+
If the condition is never met, the loop can become infinite. In Python
9+
conditional loops are defined with the ``while`` statement::
10+
11+
counter = 0
12+
while counter < 10:
13+
print "in the loop"
14+
counter += 1
15+
16+
Move until condition is met
17+
===========================
18+
19+
Exercise
20+
--------
21+
22+
Use the ``while`` loop to move the turtle forward until it reaches the end of
23+
the window.
24+
25+
Solution
26+
--------
27+
28+
::
29+
30+
def move_to_window_border(distance):
31+
while xcor() + distance < window_width() / 2:
32+
forward(distance)
33+
34+
Bonus
35+
-----
36+
37+
Can you make the turtle move exactly to the window border?
38+
39+
Draw a spiral
40+
=============
41+
42+
Loops can be interrupted with the ``break`` statement. This is often used in
43+
combination with infinite loops.
744

845
Exercise
9-
========
46+
--------
47+
48+
Write a ``while`` loop with a condition that is always true to draw a spiral.
49+
Interrupt the loop when the turtle reaches a certain distance from the center.
50+
Use the function ``distance(x, y)`` to get the turtle's distance to the point
51+
defined by the coordinates ``x`` and ``y``.
52+
53+
.. note::
54+
55+
To draw a spiral, the turtle has to rotate by a constant value and move
56+
forward by an increasing value in each repetition.
57+
58+
Solution
59+
--------
60+
61+
::
1062

63+
def draw_spiral(radius):
64+
speed = 1
65+
while True:
66+
forward(speed)
67+
left(10)
68+
speed += 0.1
69+
if distance(0, 0) > radius:
70+
break
1171

docs/en/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ Table of contents:
1414
loops
1515
functions_parameters
1616
conditionals
17-
logical_operators
1817
conditional_loops
18+
logical_operators

docs/en/logical_operators.rst

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,53 @@
11
Logical operators
2-
=========================
2+
*****************
33

4-
We can also do some more complex things with
5-
We can return the opposite of a logic operation, with **not** as a keyword in front of your expression, or combine several logic statements together with logical **and** and **or** links between them (they are usually called *operators*)
4+
Introduction
5+
============
66

7+
Conditionals are a nice way to make decisions by asking if something equals true
8+
or not. But often one condition is not enough. For instance if we want to make a
9+
decision upon ``xcor()`` and ``ycor()`` we have to combine them. This can be
10+
done with logical operators.
711

12+
This and that or something else
13+
===============================
14+
15+
Two easy to understand operators are ``and`` and ``or``. They do exactly whay the
16+
sound like: combine two statements in a way both have be true (``and``) or
17+
one of them has to be true (``or``)::
18+
19+
if 1 < 2 and 4 > 2:
20+
print "condition met"
21+
22+
if 1 < 2 and 4 < 10:
23+
print "condition not met"
24+
25+
if 4 < 10 or 1 < 2:
26+
print "condition met"
27+
28+
You are not restricted to one logical operator. You can combine as may as you
29+
want.
30+
31+
Exercise
32+
--------
33+
34+
Create a function that accepts the argument ``angel`` and moves the turtle into
35+
that direction until either the vertical or horizontal distance to the center
36+
exeeds 100.
37+
38+
Solution
39+
--------
40+
41+
::
42+
43+
def move_to_square_border(angel):
44+
left(angel)
45+
while xcor() > -100 and xcor() < 100 and ycor() > -100 and ycor() < 100:
46+
forward(10)
47+
48+
Bonus
49+
-----
50+
51+
Execute the ``move_to_square_border()`` function multiple times with different
52+
angels. Use the ``home()`` function to make the turtle jump to the center after
53+
each line.

0 commit comments

Comments
 (0)