Class 20 — Friday March 19
Decisions made easy
Put cell phones away — Please do no multi-tasking — Or under-tasking
Look both ways
Agenda
- Study the
if
statement
- Introduce searching accumulation
Program promise.py
- Possible chance to resubmit during it class.
To do list
- Really examine the artifacts
- Read epistle on decision making
- Do homework
Examples
- Program begins_with_a_vowel.py
- Program robotomy.py
- Program daisy_daisy.py
- Program spell_check.py
Last 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 — part 1
- 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.
Problem begins_with_a_vowel.py
- Report whether use text begins with a vowel. The message should be
'Text begins with vowel'
or'Text does not begin with vowel'
.
-
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
The why, what, and how of an if
statement — part 1
- There are variants of the
if
statement to make decisions based on one, two, or several possibilities.
- When we need to test several different 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 then exited.
- Otherwise, the
elif
's are evaluated one after the other until one evaluates toTrue
. Thatelif
's action is then 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 daisy_daisy.py
- Report message based on indicated day of week. The message should be
weekend day
for Sunday and for Saturday, the message should be'start of school week'
for Monday, the message should be'end of school week'
for Friday, and'school day'
, otherwise.
-
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
- 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.
- Observation
- 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 — scan through the text of interest and determine whether 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 its 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 try to interleaving 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 am I
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |