Class 19 — Wednesday March 2
Introduction to "if" and "else"
"Fall in love with the process and the results will follow" -Eric Thomas
Look both ways
Agenda
- Being logical
if
statement
else
keyword
Downloads
- Program logical_operators.py
- Program acid_test.py
- Program begins_with_a_vowel.py
- Program robotomy.py
- Program daisy_daisy.py
- Program spell_check.py
Yesterclass
- Introduced the Python relational operators:
<
,<=
,>
,>=
,==
, and!=
.
- Introduced the Python logical operators:
and
,or
, andnot
- Today, we will wrap up with two more operators:
in
, andnot in
To do
- Review artifacts
- Read epistle on decision making
Program logical_operators.py
- Introduces the logical operators
Program run
##### Logical values (bool)
True
False
<class 'bool'>
<class 'bool'>
##### 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
The why, what, and how of the basic if
statement
- General problem-solving requires code to act differently, depending upon the circumstances.
- 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.
- 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 sequences of statements (ie lines of code).
- The actions are indented one-level further than the start of the
if
statement.
- Note that the
for
loop allowed us to execute the same lines of code more than one time. Theif
andelse
statements allows us to completely skip lines of code depending on our inputs.
- That is, we choose whether or not to execute
action
1 based on some logical expression (think True/False test question).
- 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.
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
Program what_color_will_my_chrysanthemums_be.py
- Uses an
if
statement to help determine the color of a chrysanthemum based on soil pH.
-
Some program runs
Enter soil pH level: 6.5
pink
Enter soil pH level: 7
blue
Enter soil pH level: 7.5
blue
Problem begins_with_a_vowel.py
- Report whether user text begins with a vowel. The output message should be
'Text begins with vowel'
or'Text does not begin with vowel'
.
- General problem solving strategy — keep things simple.
- First idea — clean up text by stripping and converting to lower case.
-
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 or Saturday, the message should be'Start of school week'
for Monday, the message should be'End of school week'
for Friday, and'School day'
, Tuesday, Wednesday, or Thursday.
-
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
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |