Class 20 — Frdiay February 28
Decisions made easy
I am positive — My favorite color is hue — Easy decision
Put cell phones away — Please do no multitasking — Or undertasking
Look both ways
Agenda
- Introduce the
if
statement
- Searching accumulation
To do list
- Really examine the artifacts
- Read epistle on decision making
- Do homework
Examples
- Problem acid_test.py
- Problem robotomy.py
- Problem daisy_daisy.py
Last class (baby step) problem acid_test.py — building a better chrysanthemum
- Determine whether its input pH level is acidic; i.e., less than 7.0
Some program runs
Enter pH level: 6.5
True
Enter pH level: 7.0
False
Enter pH level: 7.5
False
Expanded problem solving
- Some problem solving requires the ability to react — depending upon the circumstances, different actions need to be performed.
- To support this kind of problem solving, Python provides the
if
statement.
- 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 evalutes 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 is
True
,action
1 is executed; otherwise,
action
2 is executed.
No matter what only one of the two actions are executed.
Problem what_color_will_my_chrysanthemums_be.py
- Determine chrysanthemum color given an input soil pH level, where acidic soil produces pink chrysanthemums and non-acidic soil produces blue chrysanthemums.
Some program runs
Enter soil pH level: 6.5
pink
Enter soil pH level: 7
blue
Enter soil pH level: 7.5
blue
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 who_is_the_longest_of_them_all.py
- Determine the longest word in user text. A constraint on your solution is that the only permitted list is the list of input words.
- WWAHD strategy
- Scan through the words one-at-a-time and determinewhether the current word is the longest word so far.
Some possible program runs
Enter text: Think before I act — My problem solving mantra — Make it your also
problem
Enter text: Remember always — Seek to be both great and good — The world needs your aid
Remember
Enter text:
Brainstorming
- Because we do not know the number of words to be given when we are writing our solution, we definitely need a loop that can iterate over the list of input words.
- When we process a word we need to be able to determine whether it is in contention for being the longest word.
- If it is the longest word we seen so far, then we need to remember it in order to compare it to the remaining words.
- If it is not the longest word we seen so far, we take no further interest in it.
- For such loop processing to work, we need an accumulator variable keeping track of the longest word found so far. There are two obvious candidates for initializing that variable — making it the empty string or making it the first word. Either can work and there are good arguments for both of them.
- One thing for sure, the variable keeping track of the longest word so far needs to be initialized before the loop — because during the loop we are updating it (sometimes).
Where am I
© 2020 Jim Cohoon | Resources from previous semesters are available. |