Unit 2. Developing Programming ToolsRevision Date: Jan 11, 2020 (Version 3.0)
Students will learn how programs can solve problems using the various types of conditional statements in Python programs.
Decisions in programs are made using conditional statements.
How are comparison operators and Boolean expressions used with conditional statements?
Student computer usage for this lesson is: required
In the Lesson Resources folder:
Think of a decision you make in your daily life and how you make the decision. In your journal, write about your decision and the process you use to decide.
Teaching note: take a few minutes to have students share responses (whole class, elbow partners, small groups).
==, !=, <, >, >=, <=
Includes AND, OR, and NOT
Say: The College Board’s exam reference sheet uses:
Example: Type various combinations of Boolean values (True / False) with Boolean operators (not, and, or) into the IDE (PyCharm)
Suggested Activity: What’s in the box?
Materials: Two small opaque containers, one small item for each container, two post-it notes.
Set-Up Directions: Set up the activity before the class arrives, following the directions below:
if numofGreen > numofBlack:open green box
open black box
Show the example from How to Think Like a Computer Scientist text (ActiveCode:6 (ch05_4)) or a similar example. Briefly demonstrate how to read flowcharts while showing the example from How to Think Like a Computer Scientist. Point out that the API (https://docs.python.org/3/library/) is always available as a language reference to help translate from algorithms/flowcharts into code. The API describes many tools to make it easier to create programs and is an important tool for all programmers.
If desired an if-else statement that returns boolean values can be replaced by the conditional expression or vice versa.
if (x < y):
we could use:
return x < y
Define: A code segment refers to a collection of program statements that are part of a program.
Have students answer the following questions:
1. What will be printed by the following code segment?
x=15 if x==25: print ('Pizza is yummy') else: print ('My teacher is awesome')
2. What will be printed by the following code segment?
if x!=25 and y==52:
print ('Pizza is yummy')
('My teacher is awesome')
Suggested Coding Example:
n = input('Please enter your password: ')if n=='P@s5w0d':
print ('Welcome, correct user!')
print ('Incorrect, try again')
Say: The College Board’s exam reference sheet uses if and if-else statements like Python except it uses curly braces to indicate the block of statements controlled by if or if-else statements.
1 Complete the following Runestone Activities
2 If/else practice problem: In the IDE, write a program that will prompt a user to enter a value for a food item. Evaluate the variable
food. If the value of
food is equal to “potato salad,” display “In Stock”. If the value of
food is not equal to “potato salad,” display “Not in Stock”.
Test your program with the following values for food:
Thinking about conditional execution, answer the following questions.
Alternate Instructional Strategy for Guided Practice :
Alternate Instructional Strategy for Journal: Interactive Journaling