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/average.py
  Adding /home/adas/cs21/solutions/w02-numstr/average_word.py
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
Alpha.py  isOdd.py  speeding.py
[w03-decisions]$
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>:
  <body>


if <condition>:
  <body>
else:
  <body>
  
if <condition>:
  <body>
elif <condition>:
  <body>
else:
  <body>
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 cold.py in gvim.

temp=input("Enter the current temperature in Fahrenheit: ")
coldTemp = 0
if temp <= coldTemp:
  print "Brrr!",temp,"is cold."
else:
  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 isOdd.py. 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 isOdd.py 
Please enter a number: 0
0 is even

$ python isOdd.py 
Please enter a number: 1243678
1243678 is even

$ python isOdd.py 
Please enter a number: 2371
2371 is odd
What did you type?
Next open isAlpha.py 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 isAlpha.py 
Enter a single keyboard character: 3
'3' is a decimal digit

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

$ python isAlpha.py 
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)
... 
   6.58
 138.22
 269.87
 401.51
 533.15
 664.79
 796.44
 928.08
1059.72
1191.36