Skip to content

Commit 9faef5d

Browse files
committed
Expand language introducing conditional loops
Also, changing wording on prison slightly
1 parent c646c3b commit 9faef5d

1 file changed

Lines changed: 56 additions & 32 deletions

File tree

‎source/conditional_loops.rst‎

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,66 @@
11
Conditional Loops
2-
*****************
3-
2+
*****************
43
Introduction
54
============
65

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::
6+
Conditional loops are way to repeat something while a certain condition
7+
is satisfied, or **True**. If the condition is always satisfied (never
8+
becomes **False**), the loop can become infinite.
9+
If the condition starts off false, the code in the loop will never run!
10+
In Python conditional loops are defined with the ``while`` statement::
11+
12+
word = ''
13+
sentence = ''
14+
print('Please enter some words.')
15+
print('Include a period (.) when you are finished.')
16+
while '.' not in word:
17+
word = input('next word: ')
18+
sentence = word + ' ' + sentence
19+
print()
20+
print('Aha! You said:')
21+
print(sentence)
22+
23+
We call this part of the code the 'conditional': ``'.' not in word``
24+
25+
Whether the conditional return True or not determines whether the code inside
26+
the ``while`` loop runs.
1027

11-
counter = 0
12-
while counter < 10:
13-
print("in the loop")
14-
counter = counter + 1
28+
Read the code above, and see if you can summarise in your head what
29+
it what it do (what its final output will be).
1530

31+
Then copy it into a file, say :file:`sentence.py` and run it - see
32+
exactly what it does. Does that match up with what you thought?
33+
34+
.. note::
35+
36+
If you are using Python 2, you will need to replace ``input`` with
37+
``raw_input`` to run the program correctly.
1638

1739
Turtle prison
1840
=============
1941

2042
Exercise
2143
--------
2244

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.
45+
The turtle has been up to its usual tricks again, robbing liqour
46+
stores and building up huge gambling debts. It's time for turtle to be
47+
put into a cell that it can't get out of.
2648

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
49+
Let's make a new version of ``forward()``. One that will turn the turtle
50+
around if it tries to go further than 100 from the origin. We'll need
2951
a ``while`` loop, and some new turtle functions:
3052

3153
* ``turtle.distance(0,0)`` - Distance of the turtle from the origin
3254
* ``turtle.towards(0,0)`` - The angle to get back to origin
33-
* ``turtle.setheading(angle)`` - Directly set the turtle direction
55+
* ``turtle.setheading(angle)`` - Directly sets the turtle direction
3456

35-
Now you will need to implement the prison logic using these turtle
36-
functions, a ``while`` loop and a bit of conditional logic. It's a bit
37-
of a stretch but keep at it! Don't be afraid to talk it out with a
38-
coach or another student.
57+
You could try playing with a turtle in the interpreter and using these
58+
functions to check exactly what they do, if you like.
3959

60+
Now you will need to implement the prison logic using these turtle
61+
functions, perhaps a ``while`` loop and a bit of conditional logic.
62+
It's a bit of a stretch but keep at it! Don't be afraid to talk it out
63+
with a coach or another student.
4064

4165
Solution
4266
--------
@@ -55,27 +79,27 @@ Solution
5579
Draw a spiral
5680
=============
5781

58-
Loops can be interrupted with the ``break`` statement. This is
59-
especially useful if you write an *infinite loop*, which is a loop
60-
where the conditional is always *True*.
82+
Loops can be interrupted with the ``break`` statement. This is
83+
especially useful if you write an *infinite loop*, which is a loop
84+
where the conditional is always **True**.
6185

6286
Exercise
6387
--------
6488

65-
Write a ``while`` loop with a condition that is always *True* to draw a spiral.
66-
Interrupt the loop when the turtle reaches a certain distance from the center.
67-
Use the function ``turtle.distance(x, y)`` to get the turtle's distance to the
68-
point defined by the coordinates ``x`` and ``y``.
89+
Write a ``while`` loop with a condition that is always **True**
90+
to draw a spiral. Interrupt the loop when the turtle reaches a certain distance
91+
from the center. Use the function ``turtle.distance(x, y)`` to get the
92+
turtle's distance to the point defined by the coordinates ``x`` and ``y``.
6993

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.
94+
To do this you will need the ``turtle.xcor()`` and ``turtle.ycor()``
95+
functions, which return the position of the turtle in X and Y axes
96+
respectively.
7397

7498
.. note::
7599

76100
To draw a spiral, the turtle has to rotate by a constant value and move
77101
forward by an increasing value.
78-
102+
79103
Solution
80104
--------
81105

@@ -95,6 +119,6 @@ Solution
95119
Bonus
96120
-----
97121

98-
Can you make a conditional for this loop, so you don't need the
99-
infinite loop *while True* or the *break*? Which version do you find
122+
Can you make a conditional for this loop, so you don't need the
123+
infinite loop ``while True`` or the ``break``? Which version do you find
100124
easier to understand?

0 commit comments

Comments
 (0)