Class 21 — Monday March 22
You are so controlling
- While I can go on – I do not repeat myself – Always a new way
Look both ways
Agenda
- Continue exploration of
if
statement
- Introduce
while
statement
- Review test
To do
- Examine artifacts
- Examine
while
epistle
- Prepare for next class by looking at
dict
module documentation
Examples
- Program robotomy.py
- Program yes_or_no.py
- Program guess_what.py
For your consideration
- How would you handle the problem of getting a yes or no reponse from a user, where case of the letters do not matter. Once gotten, the valid response is echoed (printed).
- Brainstorming
- Do we know ahead of time, how many often we need to loop?
- Are there action(s) that need to be done multiple times?
- Are there action(s) that need to be done once?
- Some possible runs
Enter (yes / no): YES
yes
Enter (yes / no): No
no
Enter (yes / no): yeah
Enter (yes / no): yeap
Enter (yes / no): tá
Enter (yes / no): כן
Enter (yes / no): ndiyo
Enter (yes / no): haan
Enter (yes / no): YeS
yes
Enter (yes / no): nah
Enter (yes / no): không
Enter (yes / no): nope
Enter (yes / no): aʻole
Enter (yes / no): ei
Enter (yes / no): NO
no
Whiling away
- Like many programming languages, Python provides more than one way to iterate actions (repeatedly execute). We are concerned here with its
while
looping offering.
- The
while
loop differs from thefor
loop in that the number of times it iterates is not based on the size of a list or range; instead, iteration is based on a logical test expression.
- The basic
while
state has the form:
while ( test expression ) :
action
- When Python reaches a
while
loop, it will repeatedly execute the loop's actions as long as the test expression evaluates to true. When the test expression evaluates false, the program drops down to the statement following the loop.
- FYI: Python is one of the languages whose
while
statement also allows an optionalelse
. Theelse
action is executed once the test expression evaluates to false.
while ( test expression ) :
action
else :
action
looking_for_a_yes_or_no = True
while ( looking_for_a_yes_or_no == True ) :
reply = input( 'Enter (yes / no): ' )
reply = reply.lower()
if ( reply in [ 'yes', 'no' ] ) :
looking_for_a_yes_or_no = False
print( reply )
Problem yes_or_no.py
- Solicit's a yes or no response from it's user.
Problem guess_what.py
- Play a guessing name with the user. The program is to determine what number the user has guessedl, where the possible numbers are from 0 to 100,000.
- Brainstorming
- How would you solve the problem if you were the guesser?
- Can we do this with a
for
loop? If, so what are some possible ways?
- Can we do this with a
while
loop? If, so what are some possible ways?
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
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |