if Statements

Up ’til now, we haven’t had too much control over our programs.  if statements are one
of the basic building blocks of almost any program (along with while and for, more on
those later).  These are what we we call flow control, because they give us options for
different scenarios in our program.  Let’s start with a simple example program:

number = input("Choose a number:") #Prompt the user for a number
if number >= 100:
    print "That is a high number!"
else: 
    print "That number is less than 100!

Lets examine this line by line.  The first line just prompts the user for a number.  At
the start of the if statement, it says if the users input is >= (greater than or equal to)
than 100, then do all the indented statements right under it.  The else statement is part
of the if statement, it means that if number is anything else, then do every statement
that is indented under it.  Python is sensitive to white space, so indentation is very
important, if you leave it out, you will get an error.
Here are all the various conditional operators you can use in your if statements:

== Equal to.  Make sure you don’t confuse this with =, which assigns variables.
!= Not Equal to
> Greater than.
< Less than.
>= Greater than or Equal to.  => will not work.
<= Less than or Equal to. =< will not work.

I am going to give you a more sophisticated example now, which we will edit and
improve upon in the next chapter:

print "************MENU************" #Make a menu
print "1. Add numbers"
print "2. Find perimeter and area of a rectangle"
print "0. Forget it!"
print "*" * 28
option = input("Please make a selection: ")#Prompt user for a selection 
if option == 0: #If option is 0, quit statement
    quit 
elif option == 1: #If option is 1, get input and calculate
    firstnumber = input("Enter 1st number: ")
    secondnumber = input("Enter 2nd number: ") 
    addit = firstnumber + secondnumber
    print firstnumber, "added to", secondnumber, "equals", addit #show
results
elif option == 2: #If option is 2, get input and calculate
    length = input("Enter length: ")
    width = input("Enter width: ")
    perimeter = length * 2 + width * 2
    area = length * width 
    print "The perimeter of your rectangle is", perimeter #show results
    print "The area of your rectangle is ", area
else: #if the input is anything else its not valid
    print "That is not a valid option!"

Now lets go through this one.  The first block of it is just to print the menu.  Then we
get a selection from the user.  If they choose 0 then we use the quit command, and
break out of the whole if statement (which in this case is the rest of the program). 
Meaning it won’t do anything else in that specific if statement.  The next statement
you see something new, elif.  elif is just a shortened version of “else if” if we just used
if there it wouldn’t keep the whole statement together.  It would just start a brand new
if statement.  By using elif it binds that statement to the first one.  Which is important
once we get down to else.  Because we have the whole statement
binded, else examines if it is option 0, 1, or 2 and if not prints “That is not a valid
option”.  Had we just used if on the other two statements it would have only examined
the last option (i.e. if option == 2).  So would have returned the “That is not a valid
option” output if we had chosen option 1.

Nesting if statements

We also have the ability to nest if statements.  Meaning we can have one inside
another that it will only examine and do if the first if statement is true.  Let’s tweak the
example above to give you an idea of what I mean.

if option != 0: #Only do this if the option is not 0
    if option == 1: #If option is 1, get input and calculate
        firstnumber = input("Enter 1st number: ")
        secondnumber = input("Enter 2nd number: ")
        add = firstnumber + secondnumber
        print firstnumber, "added to", secondnumber, "equals", add    #show
results
    elif option == 2:#If option is 2, get input and calculate
        length = input("Enter length: ")
        width = input("Enter width: ")
        perimeter = length * 2 + width * 2
        area = length * width
        print "The perimeter of your rectangle is", perimeter  #show
results 
    print "The area of your rectangle is", area
    else:#if the input is anything else its not valid        
        print "That is not a valid option!"

So if they choose option 0, python will not even look at anything that is indented
under it. Now your probably wondering how we can make this program go on until
we tell it to quit.  For that we need a loop…

Practice

  1. Design a program that has the user guess a password, if it is wrong say “LOCKED OUT!”