Decision structures

In class exercises
The compute average program solution has been posted to update21. Run update21 at the command prompt to get these updates
$ update21
Creating /home/adas/cs21/solutions/w02-numstr
  Adding /home/adas/cs21/solutions/w02-numstr/
  Adding /home/adas/cs21/solutions/w02-numstr/
Decision structures
We'll be looking at some in class examples for decision structures today that will require you to copy some files from my directory.

Change into your inclass directory.

[~]$ cd
[~]$ cd cs21/inclass/
[inclass]$ ls
w01-intro/  w02-numAndString/  w03-decisions/
If the w03-decisions directory does not yet exist, create it using mkdir
[inclass]$ mkdir w03-decisions
Copy the sample files for today from my directory using cp
[inclass]$ cp ~adanner/public/cs21/w03-decisions/* w03-decisions/
[inclass]$ ls
w01-intro/  w02-numAndString/  w03-decisions/
[inclass]$ cd w03-decisions/
[w03-decisions]$ ls
if, else, elif
Sometimes we need to make decisions based on various input parameters or conditions in our programs. Python supports automated decision making using if, else, elif syntax.
if <condition>:

if <condition>:
if <condition>:
elif <condition>:
The condition must be an expression that evaluations to True or False. The term Boolean is used to describe a type of data that can be either True or False.

Try a few examples of conditions in the python shell

Make sure you are in the inclass/w03-decisions directory and open in gvim.

temp=input("Enter the current temperature in Fahrenheit: ")
coldTemp = 0
if temp <= coldTemp:
  print "Brrr!",temp,"is cold."
  print "At least",temp,"is above",coldTemp
Boolean operators: <, >, <=, >=, ==, !=, and, or, not Why is == a Boolean operator instead of just =? Think syntax and semantics.
Never odd or even?
The first program we will look at is Modify the program so that it prints out if a number entered by a user is odd or even. Think about how the mod or remainder operator % can be used with an if statement to solve this problem. Some sample runs are shown below.
$ python 
Please enter a number: 0
0 is even

$ python 
Please enter a number: 1243678
1243678 is even

$ python 
Please enter a number: 2371
2371 is odd
What did you type?
Next open and modify the program so that it determines if a single character (a length 1 string) entered by the user is an uppercase letter, a lowercase letter, a decimal digit, or none of the above (usually punctuation). This program relies on python's ability to compare two strings using the operators < >, ==, <=, >=, and !=. Try it! Comparisons are done using the ASCII character codes. If you want to quickly see what happens, try a few example in a separate python shell. Some example runs of a sample program are shown below.
$ python 
Enter a single keyboard character: 3
'3' is a decimal digit

$ python 
Enter a single keyboard character: a
'a' is a lowercase letter

$ python 
Enter a single keyboard character: Q
'Q' is an uppercase letter
What if the user enters more than one character? Can you inform them politely on how to run the program correctly?
String Formatting
Until now, our only option for printing multiple data types in a single line was to use the comma.
myInt = 86
myFloat = 1./3
print "The value", myInt, "is an integer but ", myFloat, "is not"
We can get more control over how data values are displayed using string formatting. See Section 4.5.2 on page 102 for more examples.
myInt = 86
myFloat = 1./3
print "The value %d is an integer but %0.2f is not" % (myInt, myFloat)
Using this new style requires a few steps. First, set up a string that contains one or more formatting tags. All tags have the form %<width><.><precision><type-char>. The type-char is s for strings, d for integers (don't ask why), and f for floats. Do not put any variable names in the format string, just tags which serve as placeholders for data that will come later. After the format string put a single % symbol after the close quote. Finally, place the data elements that you wish to substitute for the tags separated by commas and enclosed in parentheses. Let's step through a few examples in the python shell.
>>> month="Feb"
>>> day=3
>>> year=2009
>>> print "Today is %d %s %d" % (day, month, year)
Today is 3 Feb 2009
>>> print "Tomorrow is %d/%d/%d" %(2,day+1, year)
Tomorrow is 2/4/2009
>>> for val in range(1,200,20):
...   print "%7.2f" % (val*6.582118)