﻿ Program Arcade Games With Python And Pygame

# Program Arcade GamesWith Python And Pygame

 < Previous Home Next >

# Chapter 3: Quiz Games and If Statements

How do we tell if a player has beat the high score? How can we tell if he has run out of lives? How can we tell if she has the key required to open the locked door?

What we need is the if statement. The if statement is also known as a conditional statement. (You can use the term “conditional statement” when you want to impress everyone how smart you are.) The if statement allows a computer to make a decision. Is it hot outside? Has the spaceship reached the edge of the screen? Has too much money been withdrawn from the account? A program can test for these conditions with the if statement.

## 3.1 Basic Comparisons

Here are a few examples of if statements. The first section sets up two variables (a and b) for use in the if statements. Then two if statements show how to compare the variables to see if one is greater than the other. Press the “Step” button and see how the computer evaluates the code.
 # Variables used in the example if statements a = 4 b = 5 # Basic comparisons if a < b: print("a is less than b") if a > b: print("a is greater than b") print("Done")  Variables: a=4 b=5 Output: a is less than b Done 

Since a is less than b, the first statement will print out if this code is run. If the variables a and b were both equal to 4, then neither of the two if statements above would print anything out. The number 4 is not greater than 4, so the if statement would fail.

To show the flow of a program a flowchart may be used. Most people can follow a flowchart even without an introduction to programming. See how well you can understand Figure 3.1.

This book skips an in-depth look at flowcharting because it is boring. But if you want to be a superstar programmer, please read more about it at:
http://en.wikipedia.org/wiki/Flowchart

The prior example checked for greater than or less than. Numbers that were equal would not pass the test. To check for a values greater than or equal, the following examples show how to do this:

if a <= b:
print("a is less than or equal to b")

if a >= b:
print("a is greater than or equal to b")


The <= and >= symbols must be used in order, and there may not be a space between them. For example, =< will not work, nor will < =.

When writing these statements out on a test, some students like to use the ≤ symbol. For example:
if a ≤ b:

This ≤ symbol doesn't actually work in a program. Plus most people don't know how to easily type it on the keyboard. (Just in case you are curious, to type it hold down the 'alt' key while typing 243 on the number pad.) So when writing out code, remember that it is <= and not ≤. Many people lose points on tests for this reason; don't be that person.

The next set of code checks to see if two items are equal or not. The operator for equal is == and the operator for not equal is !=. Here they are in action.

# Equal
if a == b:
print("a is equal to b")

# Not equal
if a != b:
print("a and b are not equal")

Learn when to use = and ==.

It is very easy to mix up when to use == and =. Use == if you are asking if they are equal, use = if you are assigning a value.

The two most common mistakes in mixing the = and == operators are demonstrated below:

# This is wrong
a == 1

# This is also wrong
if a = 1:
print("A is one")


Stop! Please take a moment to go back and carefully study the last two code examples. Save time later by making sure you understand when to use = and ==. Don't guess.

## 3.2 Indentation

Indentation matters. Each line under the if statement that is indented will only be executed if the statement is true:

if a == 1:
print("If a is one, this will print.")
print("So will this.")
print("And this.")

print("This will always print because it is not indented.")


Indentation must be the same. This code doesn't work.

if a == 1:
print("Indented two spaces.")
print("Indented four. This will generate an error.")
print("The computer will want you to make up your mind.")


Once an if statement has been finished, it is not possible to re-indent to go back to it. The test has to be performed again.

if a == 1:
print("If a is one, this will print.")
print("So will this.")

print("This will always print because it is not indented.")
print("This will generate an error. Why it is indented?")


## 3.3 Using And/Or

An if statement can check multiple conditions by chaining together comparisons with and and or. These are also considered to be operators just like + or - are.

# And
if a < b and a < c:
print("a is less than b and c")

# Non-exclusive or
if a < b or a < c:
print("a is less than either b or c (or both)")


A common mistake is to omit a variable when checking it against multiple conditions. The code below does not work because the computer does not know what to check against the variable c. It will not assume to check it against a.

# This is not correct
if a < b or < c:
print("a is less than b and c")


## 3.4 Boolean Variables

Python supports Boolean variables. What are Boolean variables? Boolean variables can store either a True or a value of False. Boolean algebra was developed by George Boole back in 1854. If only he knew how important his work would become as the basis for modern computer logic!

An if statement needs an expression to evaluate to True or False. What may seem odd is that it does not actually need to do any comparisons if a variable already evaluates to True or False.

# Boolean data type. This is legal!
a = True
if a:
print("a is true")


Back when I was in school it was popular to say some false statement. Wait three seconds, then shout “NOT!” Well, even your computer thinks that is lame. If you are going to do that, you have to start with the not operator. The following code uses the not to flip the value of a between true and false.

# How to use the not function
if not(a):
print("a is false")


Because not is an operator and not a function, the parentheses aren't necessary. This is also legal:

# How to use the not function
if not a:
print("a is false")


It is also possible to use Boolean variables with and and or operators.

a = True
b = False

if a and b:
print("a and b are both true")

Who knew True/False could be hard?

It is also possible to assign a variable to the result of a comparison. In the code below, the variables a and b are compared. If they are equal, c will be True, otherwise c will be False.

a = 3
b = 3
# This next line is strange-looking, but legal.
# c will be true or false, depending if
# a and b are equal.
c = a == b
# Prints value of c, in this case True
print(c)

Zero means False. Everything else is True.

It is possible to create an if statement with a condition that does not evaluate to true or false. This is not usually desired, but it is important to understand how the computer handles these values when searching for problems. The statement below is legal and will cause the text to be printed out because the values in the if statement are non-zero:

if 1:
print("1")
if "A":
print("A")


The code below will not print out anything because the value in the if statement is zero which is treated as False. Any value other than zero is considered true.

if 0:
print("Zero")


In the code below, the first if statement appears to work. The problem is that it will always trigger as true even if the variable a is not equal to b. This is because b by itself is considered true.

a = "c"
if a == "B" or "b":
print("a is equal to b. Maybe.")

# This is a better way to do the if statement.
if a == "B" or a == "b":
print("a is equal to b.")


## 3.5 Else and Else If

Below is code that will get the temperature from the user and print if it is hot.

temperature = int(input("What is the temperature in Fahrenheit? "))
if temperature > 90:
print("It is hot outside")
print("Done")


If the programmer wants code to be executed if it is not hot, she can use the else statement. Notice how the else is lined up with the i in the if statement, and how it is followed by a colon just like the if statement.

In the case of an if...else statement, one block of code will always be executed. The first block will be executed if the statement evaluates to True, the second block if it evaluates to False.

temperature = int(input("What is the temperature in Fahrenheit? "))
if temperature > 90:
print("It is hot outside")
else:
print("It is not hot outside")
print("Done")


It is possible to chain several if statements together using the else...if statement. Python abbreviates this as elif.

temperature = int(input("What is the temperature in Fahrenheit? "))
if temperature > 90:
print("It is hot outside")
elif temperature < 30:
print("It is cold outside")
else:
print("It is not hot outside")
print("Done")


In the code below, the program will output “It is hot outside” even if the user types in 120 degrees. Why? How can the code be fixed?

If you can't figure it out, see the video.

temperature = int(input("What is the temperature in Fahrenheit? "))
if temperature > 90:
print("It is hot outside")
elif temperature > 110:
print("Oh man, you could fry eggs on the pavement!")
elif temperature < 30:
print("It is cold outside")
else:
print("It is ok outside")
print("Done")


## 3.6 Text Comparisons

It is possible to use an if statement to check text.

user_name = input("What is your name? ")
if user_name == "Paul":
print("You have a nice name.")
else:


The prior example will only match if the user enters “Paul”. It will not work if the user enters “paul” or “PAUL”.

A common mistake is to forget the quotes around the string being compared. In the example below, the computer will think that Paul is a variable that stores a value. It will flag an error because it has no idea what is stored in the variable Paul.

user_name = input("What is your name? ")
if user_name == Paul: # This does not work because quotes are missing
print("You have a nice name.")
else:


### 3.6.1 Multiple Text Possibilities

When comparing a variable to multiple possible strings of text, it is important to remember that the comparison must include the variable. For example:

# This does not work! It will always be true
if user_name == "Paul" or "Mary":


# This does work
if user_name == "Paul" or user_name == "Mary":


This is because any value other than zero, the computer assumes to mean True. So to the computer "Mary" is the same thing as True and so it will run the code in the if statement.

### 3.6.2 Case Insensitive Comparisons

If the program needs to match regardless as to the case of the text entered, the easiest way to do that is to convert everything to lower case. This can be done with the lower command.

Learn to be insensitive.

The example below will take whatever the user enters, convert it to lower case, and then do the comparison. Important: Don't compare it against a string that has uppercase. If the user input is converted to lowercase, then compared against uppercase letters, there is no way a match can occur.

user_name = input("What is your name? ")
if user_name.lower() == "paul":
print("You have a nice name.")
else:


## 3.7 Example if Statements

The next set of example code below runs through all the concepts talked about earlier. The on-line video traces through each line of code and explains how it works

In the video I use an integrated development editor (IDE) called Eclipse. The default version of Eclipse doesn't work with Python, but the PyDev version does. The PyDev editor is available for free from:
http://pydev.org/

The editor is complex, but it has many options and can be a powerful environment to work in. Some programmers like using environments such as PyDev that can have so many plug-ins that will do everything but bring you coffee. Some developers prefer a minimalistic environment that doesn't “get in the way.”

# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://simpson.edu/computer-science/

# Explanation video: http://youtu.be/pDpNSck2aXQ

# Variables used in the example if statements
a = 4
b = 5
c = 6

# Basic comparisons
if a < b:
print("a is less than b")

if a > b:
print("a is greater than than b")

if a <= b:
print("a is less than or equal to b")

if a >= b:
print("a is greater than or equal to b")

# NOTE: It is very easy to mix when to use == and =.
# Use == if you are asking if they are equal, use =
# if you are assigning a value.
if a == b:
print("a is equal to b")

# Not equal
if a != b:
print("a and b are not equal")

# And
if a < b and a < c:
print("a is less than b and c")

# Non-exclusive or
if a < b or a < c:
print("a is less than either b or c (or both)")

# Boolean data type. This is legal!
a = True
if a:
print("a is true")

if not a:
print("a is false")

a = True
b = False

if a and b:
print("a and b are both true")

a = 3
b = 3
c = a == b
print(c)

# These are also legal and will trigger as being true because
# the values are not zero:
if 1:
print("1")
if "A":
print("A")

# This will not trigger as true because it is zero.
if 0:
print("Zero")

# Comparing variables to multiple values.
# The first if statement appears to work, but it will always
# trigger as true even if the variable a is not equal to b.
# This is because "b" by itself is considered true.
a = "c"
if a == "B" or "b":
print("a is equal to b. Maybe.")

# This is the proper way to do the if statement.
if a == "B" or a == "b":
print("a is equal to b.")

# Example 1: If statement
temperature = int(input("What is the temperature in Fahrenheit? "))
if temperature > 90:
print("It is hot outside")
print("Done")

# Example 2: Else statement
temperature = int(input("What is the temperature in Fahrenheit? "))
if temperature > 90:
print("It is hot outside")
else:
print("It is not hot outside")
print("Done")

# Example 3: Else if statement
temperature = int(input("What is the temperature in Fahrenheit? "))
if temperature > 90:
print("It is hot outside")
elif temperature < 30:
print("It is cold outside")
else:
print("It is not hot outside")
print("Done")

# Example 4: Ordering of statements
# Something with this is wrong. What?
temperature = int(input("What is the temperature in Fahrenheit? "))
if temperature > 90:
print("It is hot outside")
elif temperature > 110:
print("Oh man, you could fry eggs on the pavement!")
elif temperature < 30:
print("It is cold outside")
else:
print("It is ok outside")
print("Done")

# Comparisons using string/text
# Note, this example does not work when running under Eclipse
# because the input will contain an extra carriage return at the
# end. It works fine under IDLE.
print("You have a nice name.")
else: