Python, part 6

More on flow control.  Python also has an elif control--this
means else-if and can come in handy at times.  Consider the
following task.  We want to ask about how many hours a student
has completed at UTK.  We'll presume that <30 means a freshman,
30-59 means a soph, 60-89 a junior, and >= 90 a senior.
     hours = input("please type in the number of hours completed:")
     if hours < 30:
             print "freshman"
     elif hours < 60:
             print "soph"
     elif hours < 90:
             print "junior"
             print "senior"

Again--elif means else-if.  So you reach the elif hours < 90 only
when the if hours < 30 was false and the elif hours < 60 was false as
well.  Note that the final test is an else--not an elif. Note also how
this is indented.   We could also have done:
      hours = input("please type in the number of hours completed")
      if hours >= 90:
            print "senior"
             if hours >= 60:
                     print "junior"
                     if hours >= 30:
                             print "soph"
                              print "freshman"

And there are also other ways we could have structured this.  Which
code do you prefer?  Which is easier to write?  Which is easier to

A NOTE:  we have not worried about what is called defensive
Suppose you had type in as hours a value of -29.
What does this mean?  That you haven't graduated from high school?
That it will take a year for you just to reach the freshman level?  In
real life, a programmer must also take into account that a person who
runs the code may make errors--either inadvertent errors of errors
from a lack of understanding about what data the program expects.

Python's while control.  This is one of the ways that Python lets you
work with loops.  A loop is a block of statements that usually needs to
executed some number of times--maybe 0 times:
         while <condition>:
As long as the condition is true, the block of statements under the
while continues to execute.  A warning here!  This does NOT mean
that when the condition becomes false that you exit the loop.  The way
a while works is that the condition is tested (e.g. total < 1000) for being
true.  If the condition is true,  the statements in the block get executed,
and when the end of the block is reached, then and only then does the
condition get tested again--if the condition becomes false in the middle
of the block, the rest of the statements in the block are still executed.
If, when the condition is tested, the condition evaluates to false, we leave
the while and its block and continue on.  IMPORTANT NOTE:  it should
be obvious here that you MUST ensure that something happens so that the
condition will sooner or later become false.  For example:

            bad!                                                          good!
        -------                                                    -------
   number = 100                                                number = 100
   while number > 0:                                        while number > 0:
        print "the number is still >0"                     print "number still > 0"
                                                                                 number = number -1

In the first case, number will remain at 100--nothing changes it, and
we get stuck in the loop.  In the second case, number decrements each
time through the loop--eventually the condition becomes false.

          number = 100
          while number > 0:
                 print "number still > 0"
                 number = number - 1
                 print "we just decremented number!"

Remember that the loop does not terminate as soon as the condition
becomes false--here the condition becomes false in the middle of the
block--but Python does NOT test the condition again until we reach
the end of the block.  Suppose instead of number = 100 above we had
instead number = -10.  In this case the condition is tested--the result
is false, and the block never executes, not even once.

Python's for loops.  We often have a need to execute the statements in
a block some particular number of times--e.g. 10 times.  For example:

         for i in range(10):
                 statements in block

The variable i here is known as a counter variable.  The way this version
(there are other versions) of the for loop works is that Python starts the
variable i off with the value 0 and we chug along through the block.
When we reach the end of the block, Python increments i to 1 and we go
through the block again.  We will go through this block 10 times--BUT
NOTE:  the last time we go through the block i has the value 9, not 10.
If we started with i being 0 and finished with i being 10, we would have
gone through the block 11--not 10--times.  The statements in the block
may make use of the value of i--e.g.

         number = 0
         for i in range(5):
                  number = number + i

Think about what this does!  There is nothing magic about the variable i.
We could have called the counter j, or ctr, or counter_variable, etc.  We also
have more options.  range(start,finish,increment) such as range (10, 0, -1)
tells Python what initial value to give the counter, when to stop, and what
the increment (or decrement) will be.

       for i in range(10, 0, -1):   means start i with the value 10, decrement i
by 1 when we reach the end of the loop, and when, after decrementing, i
has the value 0 (or less) then exit the loop.  So range(10, 0, -1) also goes
through the loop 10 times (not 11).  range(10, 0, -5) makes the increment
-5 (i.e. a decrement).  How many times through the loop now?
range(0, 5, 1) is actually the same as the commonly abbreviated range(5).
range(0.0, 1.0, 0.01) goes through the loop how many times?  range(1, 5, 1)
goes through 4 times, not 5--why?

IMPORTANT NOTE:  suppose we try:

             number = 0
             for i in range(10):
                    number = number + i
                    i = i - 1

How many times through the loop now?  This is NOT a good idea!  It
is a very good habit NOT to mess with the counter variable this way in
the loop!