In this section we look if statements and making decisions based on certain conditions. For instance, if a player is pressing the joypad left button then we want to respond by moving the player’s character in that direction.
Also covered in this chapter are comparison operators and logical operators, both of which use used with if statements.
When writing programs there’s often the need to make the computer perform commands only when certain conditions are met. In the example game, we want to create a bullet only if the player presses the fire button and only if the bullet doesn’t already exist (the player can only fire one at a time.) Do do this, we use if statements together with conditional and logical operators.
Take a look at this code:
if 1 == 1:
print ("1 does equal 1 so this print function is executed")
The first line tests if the number 1 is equal to the number 1, which of course it is, so the following indented statement is executed by the computer. To test if the numbers are the same, the equality operator == is used (this shouldn’t be confused with the assignment operator which uses just one equal sign.) If the comparison is true then the following indented line or lines, known as a block will be run by the computer, if it is false then the block will be ignored. A comparison can result in only one of two outcomes – it’s either true or false.
In this code the print function won’t be performed because the comparison is untrue or false:
if 1==2:
print ("this print command will not run because 1 is not equal to 2")
If statements become more useful with the comparison of variables:
intLives = 1
intLives = intLives - 1
if intLives == 0:
print ("you have no lives left..")
print ("GAME OVER")
print (“this line is always run because it’s not part of the intended if block”)
The final print statement is outside of the indented if block and isn’t part of the comparison, so it’s always processed.
INDENTING WITH TABS OR SPACES?
You can use either tabs or spaces to create the indentation and mark a block of code, but it’s important to be consistent and use the same format throughout your program (or the Python interpreter will cough up errors.) In this tutorial I use 4 spaces, so I recommend doing the same if you intend to copy and paste the programs (if not you’ll need to convert the spaces into tabs.)
IF … ELSE
You can make the computer do something if a condition is met, and make it do something else if the condition is not true. This is using the else keyword after an if statement:
intAge = 10
if intAge >= 16 :
print ("you’re old enough to drive")
else:
print ("you’re under 16")
print ("you’re too young to drive")
In the above example the computer prints ‘you’re too young to drive.’ The >= operator means greater than or equal, and intAge is 10 so it’s not greater than or equal to 16, therefore the else block is executed.
LOGICAL OPERATORS
So far you’ve seen how if statements test whether a single comparison is met, but it’s possible to test for more than one condition using logical operators.
strKeyPress = "spacebar"
booBulletActive = False
if strKeyPress = "spacebar" and booBulletActive == True:
print ("you fire a bullet")
# insert code here to create a bullet
Here you see the and operator used with the if statement to test for two conditions, the first whether the player pressed the fire button (the space bar) AND secondly whether the bullet already exists (the player can’t fire more than one bullet at a time.) Only if both comparisons are true will the computer display ‘you fire a bullet.” Note that it doesn’t matter in which order you make the tests, the result will be the same.
booMouseClickClose = True
if booMouseClickClose == True or strKeyPress = "escape":
print ("you’ve exited the game")
quit ()
Here the or operator is used to test if one of two comparisons is true – whether the player mouseclicks to close the window OR presses the escape key. Only one or the other has to be true for the block of code to execute, it will also run if both are true. Like the and operator it doesn’t matter in which order you make the tests, the result is exactly the same ie:
…
if strKeyPress = “escape” or booMouseClickClose == True:
…
The final logical operator is the not keyword and what this does is reverse (or complement) the result of the comparison, as its name implies. True becomes False and False becomes True.
BooPlayerAlive = False
if not (booPlayerAlive == True) :
print ("player is not alive")
print ("GAME OVER")
Here we look at the comparison booPlayerAlive == True because it will be tested first due to the brackets giving it highest priority. In the first line of the code, booPlayerAlive is set to False, so testing if it’s equal to true will give a result of False. However, the not keyword complements the False so that it becomes True, in which case the computer will execute the print statements to display the game over messages.
A SHORTHAND METHOD OF COMPARISON
You’ve seen how if statements and comparison operators can be used to test if a condition is true or not, for example:
booPlayerAlive = True
if booPlayerAlive == True:
print ("you’re still alive")
# insert code here to play game
The comparison which is part of the if statement can be shortened to just if booPlayerAlive and will function exactly the same. The variable booPlayerAlive will be evaluated by the computer and give a result of True or False depending on its value. Here the value was set to True in the first line, so the if statement results in True and ‘you’re still alive’ is displayed on the screen.
The following does exactly same as the program above:
booPlayerAlive = True
if booPlayerAlive:
print ("you’re still alive")
# insert code here to play game
There’s a lot of information presented in this chapter in a small amount of space. Don’t be too concerned with trying to understand it all after a first reading, it takes time and practice for it to sink in. Some of the examples are quite complicated, but the more you use them the less difficult to understand they should become.
COMPARISON OPERATORS
You’ve seen one comparison operator already, the equality operator == which tests if the values on either side of it are equal (these values are known as operands.)
The other comparison operators are:
!= not equal, is true if the two operands are not equal to each other
> greater than, is true if the first operand is greater than the second
< less than, is true if the first operand is less than the second
<= less than or equal, is true if the first operand is less than or equal to the second
>= greater than or equal, is true if the first operand is greater than or equal to the second