More Control Flow
CS 110
1
Reminders and Announcements
This Week
3
Top-Down
When we first started our class, the programs we wrote went from top to bottom.
Function Definitions
When we started defining our own functions, we began to teach the computer how to do tasks which gave us the ability to first DEFINE then later CALL those functions in our programs.
Conditionals
With conditionals, we can learn how to run parts of our programs only if certain conditions were met.
Loops
Loops allow us to repeat bits of our programs many times without having to know in advanced how many pieces of data we'd have to deal with..
Events
Events allow us to tell the computer to LISTEN for particular events or inputs and when the computer HEARS that input, they run part of our program.
Control Flow in Python
If-Else
4
if condition:
statement 1
statement 2
...
CONDITION�Expression that evaluates to True or False.
Indented BLOCK�If the condition evaluates to True, the block executes. Otherwise, the block is skipped.
else:
statement 1
statement 2
...
If-Else Statement
5
my_num = int(input("Input a number…"))
if my_num == 15:
print("You win!")
else:
print("Uh oh.")
Thing to do if False
else keyword tells Python the "we're a continuation of the previous conditional
What if we want to repeat an action over and over and over again?
6
Wouldn't it be nice if we could ask Python to repeat some tasks over and over again.
7
8
Top-Down
When we first started our class, the programs we wrote went from top to bottom.
Function Definitions
When we started defining our own functions, we began to teach the computer how to do tasks which gave us the ability to first DEFINE then later CALL those functions in our programs.
Conditionals
With conditionals, we can learn how to run parts of our programs only if certain conditions were met.
Loops
Loops allow us to repeat bits of our programs many times without having to know in advanced how many pieces of data we'd have to deal with..
Events
Events allow us to tell the computer to LISTEN for particular events or inputs and when the computer HEARS that input, they run part of our program.
Control Flow in Python
Some common scenarios
How do I...
9
Repeated Actions
10
Repeated Actions
if there are oreos:
# eat an oreo
11
Repeated Actions
if there are oreos:
# eat an oreo
12
Repeated Actions
if there are oreos:
# eat an oreo
But do you really want to eat just one?
13
Repeated Actions
if there are oreos:
# eat an oreo
if there are oreos:
# eat an oreo
if there are oreos:
# eat an oreo
if there are oreos:
# eat an oreo
if there are oreos:
# eat an oreo
But what if you don’t know how many oreos you have?
14
Repeated Actions
if there are oreos:
# eat an oreo
# because no one can eat just one
while there are oreos:
# eat an oreo
15
Repeated Actions
if there are oreos:
# eat an oreo
# because no one can eat just one
while there are oreos:
# eat an oreo
16
Repeated Actions
if there are oreos:
# eat an oreo
# because no one can eat just one
while there are oreos:
# eat an oreo
17
Repeated Actions
if there are oreos:
# eat an oreo
# because no one can eat just one
while there are oreos:
# eat an oreo
18
Repeated Actions
if there are oreos:
# eat an oreo
# because no one can eat just one
while there are oreos:
# eat an oreo
19
Repeated Actions
if there are oreos:
# eat an oreo
# because no one can eat just one
while there are oreos:
# eat an oreo
20
Repeated Actions
if there are oreos:
# eat an oreo
# because no one can eat just one
while there are oreos:
# eat an oreo
21
...until they’re gone :(
22
While Loops
while some condition is True:
# execute code block
Pseudocode
23
When you want to repeat an action while a condition is true
While Loop Syntax
24
while condition:
statement 1
statement 2
...
CONDITION�Boolean expression that evaluates to True or False.
INDENTED BLOCK�While the condition evaluates to True, the block will continue to execute over and over. Otherwise, the block is skipped.
Print the Statement
How do we print this sentence over and over again?
print("Hey there! Hope you're doing OK!")
25
Demo: 01_while_simple.py
Print the Statement
Solution: How do we print this sentence over and over again?
while 5 == 5: # the condition is always True
print("Hey there! Hope you're doing OK!")
26
Demo: 01_while_simple.py
How do I make it print 10 times and then stop?
To do this, you have to answer two questions:
27
Demo: 01_while_simple.py
Using variables to track state
28
Using variables to track state
What needs to change to get it to only print 10 times?
while 5 == 5: # the condition is always True
print("Hey there! Hope you're doing OK!")
29
Demo: 01_while_simple.py
Using variables to track state
What needs to change to get it to stop after 10 "iterations"?
counter = 0
while counter < 10:
print("Hey there! Hope you're doing OK!")� counter = counter + 1
print("Counter: " + str(counter))
Increment counter each cycle
Initialize iteration variable
Specify halt condition
30
Imagine you're a political pollster
31
Your job is to call a bunch of people and give them a survey over the phone. One day you're asked to write a manual for a new employee on the procedure for calling.
Imagine you're a political pollster
32
Your job is to call a bunch of people and give them a survey over the phone. One day you're asked to write a manual for a new employee on the procedure for calling.
Which of the following approaches makes more sense:
Style 1
Style 2
Loop - Phone Call Bot
33
list_of_numbers = [
"505-503-4455",
"618-625-8313",
"719-266-2837",
"951-262-3062"
]
# use the make_call function to call each person.
# make_call takes in one input, a string that's a phone num
Loop - Phone Call Bot
34
list_of_numbers = [
"505-503-4455",
"618-625-8313",
"719-266-2837", �"951-262-3062"
]
make_call(list_of_numbers[0])
make_call(list_of_numbers[1])
make_call(list_of_numbers[2])
make_call(list_of_numbers[3])
For Loops
for each thing in a sequence:
# do some stuff
Pseudocode
35
When you want to go through a sequence of data…
for thing in my_sequence:
statements…
36
Keyword for marks the start of for loop
An arbitrary variable name to store each item in the list as we go through it
Any sequence �(tuple, list, set, string)
loop body has 1 or more statements, which have same indentation level
For Loop: “Keep retrieving the next item in the sequence, starting from the beginning, until you reach the end”. Visualize for loop.
Keyword in tells Python we're ready to specify our sequence of data
Loop - Phone Call Bot
37
list_of_numbers = [
"505-503-4455",
"618-625-8313",
"719-266-2837", �"951-262-3062"
]
for phone_num in list_of_numbers:
make_call(phone_num)