Class 19 – Friday October 11
Decisions made easy
I am positive — My favorite color is hue — Easy decision
Look both ways
Agenda
- Gain an understanding of the Python relational and logical operators
- Introduce the
if
statement
Problems
- Program can_you_relate.py
- Program logical_operators.py
- Program acid_test.py
- Program robotomy.py
- Program daisy_daisy.py
To do list
- Really examine the artifacts
- Read epistle on decision making
- Do homework 17 and 18
Expanded problem solving
- Some problem solving requires the ability to react — depending upon the circumstances different conditions need actions to be performed. Python provides the
if
statement for decision making.
- 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.
Riddle me this — what classrooms are viable for scheduling CS 1112
- The School of Engineering and Applied Science (SEAS) has four-tiered classrooms: MEC 205, Olsson Hall 120, Rice 130, and Thornton E316. All four rooms provide WIFI access. Only MEC 205, Olsson Hall 120, and Rice 130 support classroom sizes greater than 100. MEC 205 and Olsson 120 do not provide student power ports.
- The registrar has rules about scheduling 75-minute classes. They can meet anytime on Tuesdays or Thursdays. On Mondays, Wednesdays, or Fridays they cannot be scheduled before 2:00 PM.
- Surveys indicate that on average, students preferred classes whose starting times do not occur before 10:00 AM or after 2:00 PM.
- Tiered seating allows for a better eye contact between an instructor and other members of the class. Better eye contact encourages student engagement.
- Jim Cohoon has several general teaching preferences.
- Maximize student access to his courses.
- Class sessions be 75-minutes.
- 1xxx and 2xxx level courses meet three times a week.
- 3xxx and 4xxx courses meet twice a week.
- Classrooms should provide student access to WIFI and to power ports.
- Maximize student engagement.
Being logical
- Program can_you_relate.py
- Introduces the comparison operators
Three program runs
Enter number: 12
Enter number: 11
12 < 11 : False
12 > 11 : True
12 <= 11 : False
12 >= 11 : True
12 == 11 : False
12 != 11 : True
Enter number: 3
Enter number: 14
3 < 14 : True
3 > 14 : False
3 <= 14 : True
3 >= 14 : False
3 == 14 : False
3 != 14 : True
Enter number: 7
Enter number: 7
7 < 7 : False
7 > 7 : False
7 <= 7 : True
7 >= 7 : True
7 == 7 : True
7 != 7 : False
- Program logical_operators.py
- Introduces the logical operators
Program run
#### Logical values (bool)
True
False
##### Logical and
True and True = True
True and False = False
False and True = False
False and False = False
##### Logical or
True or True = True
True or False = True
False or True = True
False or False = False
##### Logical negation
not True = False
not False = True
##### Logical includes
1 in [3, 1, 4, 1, 5] = True
2 in [3, 1, 4, 1, 5] = False
##### Logical exclusion
1 not in [3, 1, 4, 1, 5] = False
2 not in [3, 1, 4, 1, 5] = True
Building a better chrysanthemum
- Program acid_test.py
- Determine whether its input pH level is acidic; i.e., less than 7.0
Three program runs
Enter pH level: 6.5
True
Enter pH level: 7.0
False
Enter pH level: 7.5
False
- Determine chrysanthemum color given an input soil pH level, where acidic soil produces pink chrysanthemums and non-acidic soil produces blue chrysanthemums.
Three program runs
Enter soil pH level: 6.5
pink
Enter soil pH level: 7
blue
Enter soil pH level: 7.5
blue
Program 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.
Seven 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
Program 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.
Three program runs
class="coding">
Door (locked / unlocked): locked
Turn around
class="coding">
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
© 2019 Jim Cohoon | Resources from previous semesters are available. |