|
| 1 | +Conditional statements |
| 2 | +********************** |
| 3 | +Introduction |
| 4 | +============ |
| 5 | + |
| 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 | + |
| 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*: *=,>,<*. |
| 15 | + |
| 16 | + |
| 17 | +Examples |
| 18 | +======== |
| 19 | +Here are some simple examples:: |
| 20 | + |
| 21 | + condition = True |
| 22 | + if condition: |
| 23 | + print "condition met" |
| 24 | + |
| 25 | + if not condition: |
| 26 | + print "condition not met" |
| 27 | + |
| 28 | + direction = -30 |
| 29 | + if direction > 0 : |
| 30 | + forward(direction) |
| 31 | + else: |
| 32 | + left(180) |
| 33 | + forward (-direction) |
| 34 | + |
| 35 | + |
| 36 | +Condition an action |
| 37 | +=================== |
| 38 | + |
| 39 | +Exercise |
| 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 | + |
| 95 | + |
| 96 | + |
0 commit comments