Class 19 — Wednesday, October 6
Decisions made easy
Put cell phones away — Please do no multitasking — Or under-tasking
Look both ways
Agenda
- Study the
if
statement
- Introduce searching accumulation
To do list
- Truly examine class artifacts
- Read epistle on decision making
- Do homework
Examples (redownload)
- Program begins_with_a_vowel.py
- Program daisy_daisy.py
- Program robotomy.py
- Program spell_check.py
Yesterclass
- Introduced the Python relational operators:
<
,<=
,>
,>=
,==
, and!=
.
- Introduced the Python logical operators:
and
,or
,not
,in
, andnot in
- Used relational operator
<
to help determine whether a soil pH sample was acidic.
- Introduced
if
statement to help determine the color of a chrysanthemum based on soil pH.
Enter soil pH level: 6.5
pink
Enter soil pH level: 7
blue
Enter soil pH level: 7.5
blue
The why, what, and how of an if
statement — revisited and expanded.
- General problem-solving requires depending upon the circumstances, to perform different actions.
- To support this kind of problem solving, Python provides the
if
statement. Python does not provide any other decision statements.
- The
if
statement uses logical test expressions to determine which action to perform.
- The most common form of the
if
statement is
if ( logical expression ) :
action1
else :
action2
- Where
logical expression
is a test expression that evaluates to eitherTrue
orFalse
.
- Both
action
1 andaction
2 are non-empty sequence of statements.
- The actions are indented one-level further than the start of the
if
statement.
- The
else
is indented at the same level as theif
. Bothif
andelse
are keywords:
- The semantics of the
if
statement begins with thelogical expression
being evaluated.
- If the expression evaluates to
True
,action
1 is executed. Otherwise,action
2 is executed.
- No matter what only one of the two actions of an
if-else
statement is executed.
- The
else
part of theif
statement is optional.
if ( logical expression ) :
action
- If the logical expression evaluates to
True
, the action is executed. Otherwise, no action occurs.
- There are variants of the
if
statement to make decisions based on one of several possibilities.
- When we need to test several conditions to determine the appropriate action to take, we can use
elif
's to test for the various possibilities (when you see anelif
think else if ).
if ( logical expression1 ) :
action1
elif ( logical expression2 ) :
action2
...
elif ( logical expressionk ) :
actionk
else : # optional
actionk+1
- The semantics of the
if-elif-if
statement begins withlogical expression
1 being evaluated.
- If the logical expression evaluates to
True
, its action is executed and theif
statement is exited.
- Otherwise, the
elif
's are evaluated one after the other until one evaluates toTrue
. Thatelif
's action is executed and theif
statement is exited.
- If none of the
elif
's logical expressions evaluate toTrue
,
- If there is an
else
part, then its action is executed and theif
statement is exited.
- If there is no
else
part, no action is taken.
- No matter what only one of the actions of an
if-elif-else
statement is executed.
Problem begins_with_a_vowel.py
- Report whether user text begins with a vowel. The output message should be
'Text begins with vowel'
or'Text does not begin with vowel'
.
- General problem solving strategy — keep things simple.
- First idea — clean up text by stripping and converting to lower case.
-
Some program runs
Enter text: apple
Text begins with vowel
Enter text: banana
Text does not begin with vowel
Enter text: Orange
Text begins with vowel
Problem daisy_daisy.py
- Report message based on indicated day of week.
- The message should be
'Weekend day'
for Sunday or Saturday, the message should be'Start of school week'
for Monday, the message should be'End of school week'
for Friday, and'School day'
, Tuesday, Wednesday, or Thursday.
-
Some program runs
Enter day of week: Sunday
Weekend day
Enter day of week: monday
Start of school week
Enter day of week: TUESDAY
School day
Enter day of week: Wednesday
School day
Enter day of week: Thursday
School day
Enter day of week: Friday
End of school week
Enter day of week: Saturday
Weekend day
Problem robotomy.py
-
Task — determine what happens when a robot comes knocking on a door
- If the door is locked, the robot should be instructed to turn around.
- If the door is instead unlocked, the robot should be instructed to open the door and enter the room. Before entering, the robot should determine whether the light is off. If it is off, the robot should be instructed to first turn on the light.
-
Observations
- There is one procedure to perform if the door is locked, another to perform if it is unlocked
- The procedure to perform if the door is unlocked, requires analysis whether the light is on.
-
Some program runs
Door (locked / unlocked): locked
Turn around
Door (locked / unlocked): Unlocked
Open the door
Light (on / off): on
Enter the room
Door (locked / unlocked): unlocked
Open the door
Light (on / off): Off
Turn on the light
Enter the room
Problem spell_check.py
- Determine misspelled words in user text. To support this activity, there is a GitHub-acquired web file with the 10,000 most commonly used English words on the webs. For those interested, a more-complete list of 173,541 English words is available.
- WWAHD (what would a human do) strategy — scan through the user text and determine whether the current word is not in the correct-spelling word list.
-
Some program runs
Enter text: It was the beste of times It was the wurst of times It was Oktoberfest
beste
wurst
oktoberfest
Enter text: how much wod can a woodchuck chuck
wod
woodchuck
Enter text: to be or not to be that is the question
Enter text:
-
Brainstorming
- To accomplish the task there are two streams of information to process.
- One stream is the input text; the other stream is the list of word spellings. Processing these streams requires different actions, so we need to be sure when we read from a stream, that we pick the right one.
- To make a decision on a word, we need to know if it’s in the list of correctly-spelled words. This fact tell us that we should get the list of word spellings before we get the user text — it would not really help to get the words first, and it would be a real mess to interleave the stream reading.
- Together, the above tell us a plan of action.
- Import our
url
module.
- Specify the URL for the spelling dictionary.
- Read the contents of the URL and store it as a list of word spellings.
- Get the user text and convert it into a list of words.
- For each user word, determine whether it is correctly spelled. If it is incorrectly spelled, print it out.
Decision making
Where in the world am I