Class 19 — Wednesday October 7
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
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 what_day_is_it.py
Last class
- Introduced the Python relational operators:
<
,<=
,>
,>=
,==
, and!=
.
- Introduced the Python logical operators:
and
,or
,not
,in
, andnot in
- Determine whether its input pH level is acidic; i.e., less than 7.0
- Used relational operator
<
to help determine whether a soil pH sample was acidic.
Enter pH level: 6.5
True
Enter pH level: 7.0
False
Enter pH level: 7.5
False
- 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
- 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.
- There are variants of the
if
statement to make decisions based on one, two, or several possibilities.
- 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 statement lists.
- 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.
- 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 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
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
Where am I
© 2020 Jim Cohoon | Resources from previous semesters are available. |