Conditional statementsĀ¶

IntroductionĀ¶

So far we have accomplished predefined tasks, but in all honesty we were accomplishing no better than old-time music boxes following one set of instructions to the end. What makes programming so much more powerful are conditional statements. This is the ability to test a variable against a value and act in one way if the condition is met by the variable or another way if not. They are also commonly called by programmers if statements.

To know if a condition is True of False, we need a new type of data: the booleans. They allow logical operations. A logic statement or operation can be evaluated to be True or False. Our conditional statement can then be understood like this:

if (a condition evaluates to True):

then do these things only for ā€˜Trueā€™

else:

otherwise do these things only for ā€˜Falseā€™.

The condition can be anything that evaluates as True or False. Comparisons always return True or False, for example == (equal to), > (greater than), < (less than.)

The else part is optional. If you leave it off, nothing will happen if the conditional evaluates to ā€˜Falseā€™.

ExamplesĀ¶

Here are some examples. You may want to read them over line-by-line and see what you think they do, or run them to be certain:

condition = True
if condition:
    print("condition met")

if not condition:
    print("condition not met")

# Only draw in green if we're going a long way
distance = 200
if distance > 100:
    turtle.pencolor("green")
    turtle.forward(distance)
else:
    turtle.pencolor("black")
    turtle.forward(distance)

Giving DirectionsĀ¶

Python turtles can be very good at following instructions. Letā€™s use the input() function to ask the user for a direction to move the turtle. To keep things easy we will only accept two instructions: ā€œleftā€ and ā€œrightā€.

Note

Using Python 2? The input() function is called raw_input().

Itā€™s much easier to define this as a function, like so:

def move():
    direction = input("Go left or right? ")
    if direction == "left":
        turtle.left(60)
        turtle.forward(50)
    if direction == "right":
        turtle.right(60)
        turtle.forward(50)

Now whenever you use move() you are prompted to choose left or right.

ā€œdata mungingā€Ā¶

In this program, the turtle will only respond to exactly left or right with no variation. Though Left or LEFT might seem the same as left to a human, that isnā€™t the case when programming. Python has a few utility methods to help with that. A string has the methods .strip(), which removes whitespace and .lower() which makes everything lower-case.

Here are some examples to print out the effects of .strip() and .lower():

my_variable = "       I Am Capitalised"
print(my_variable)
my_stripped = my_variable.strip()
print(my_stripped)
my_lower = my_variable.lower()
print(my_lower)

Try adding direction = direction.strip().lower() to the move() function, to see the effect. We often call this kind of code ā€œdata mungingā€, and it is very common.

Can you add some extra input choices to make the turtle draw other things? How about hexagon?