Class 20 — Friday March 4
"if" and "else" and Range
Look both ways
Agenda
- Being logical
if
statement
else
keyword
Downloads
- Program robotomy.py
- Program spell_check.py
- Program arrangements.py
To do
- Review artifacts and/or course recordings on Collab
- Read epistle on decision making
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
Problem arrangements.py
- Fun fact: a
range
is a type of sequence. Other sequences we have used have been strings and lists.
- Up to now whenever we have had a
range()
loop, we always had two arguments handed torange()
.range()
can also handle having one or three arguments.
- If a single argument
n
is given torange()
, then Python treats it asrange(
0,
n
)
.
- If three arguments are given to
range()
as inrange(
m,
n,
k
)
, then
- If
k
is positive, then
- The start value is
m
; the stop value is less thann
; and the other values go up in steps ofk
- If
k
is negative, then
- The start value is
m
; the stop value is greater thann
; and the other values go down in steps ofk
- Another fun fact: we can a
range
sequence into a list sequence using the built-in functionlist()
on arange
.
-
Some program runs
Enter integers m, n, and k: 3 8 2
i: 3
i: 5
i: 7
list( range( m, n, k ) ): [3, 5, 7]
Enter integers m, n, and k: 10 6 -3
i: 10
i: 7
list( range( m, n, k ) ): [10, 7]
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |