Lecture 13 Slides - Loops!

1 of 37

More Control Flow

CS 110

1

2 of 37

Reminders and Announcements

  • Exercise 5 available and due Friday (animation)
  • Grading updates:
    • Quiz 1 grades available wednesday at the very latest. Current median: ~87
    • Remember we wrote a grade calculator way back in Tutorial 1!
    • Grading Policies (weights; etc.) available in Syllabus
  • Reminder: Drop deadline is this Friday
    • If you're thinking of dropping, please come see me or a course staff member to discuss

This Week

  • Monday - Iteration (Loops)
  • Wednesday - Intro to Animation (Pre-Recorded)/ MQ 8 + Tutorial 4
  • Friday - Events and Listeners + MQ 9

3 of 37

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

4 of 37

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

...

5 of 37

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

6 of 37

What if we want to repeat an action over and over and over again?

6

7 of 37

Wouldn't it be nice if we could ask Python to repeat some tasks over and over again.

7

8 of 37

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

9 of 37

Some common scenarios

How do I...

  • Keep playing the beat over and over again?
  • Animate my creature?
  • Search through the data for keywords?
  • Render all of the photos in a list?
  • Draw 1,000 creatures?
  • Play all the notes in a list?
  • Find the biggest number in a list?

9

10 of 37

Repeated Actions

10

11 of 37

Repeated Actions

if there are oreos:

# eat an oreo

11

12 of 37

Repeated Actions

if there are oreos:

# eat an oreo

12

13 of 37

Repeated Actions

if there are oreos:

# eat an oreo

But do you really want to eat just one?

13

14 of 37

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

15 of 37

Repeated Actions

if there are oreos:

# eat an oreo

# because no one can eat just one

while there are oreos:

# eat an oreo

15

16 of 37

Repeated Actions

if there are oreos:

# eat an oreo

# because no one can eat just one

while there are oreos:

# eat an oreo

16

17 of 37

Repeated Actions

if there are oreos:

# eat an oreo

# because no one can eat just one

while there are oreos:

# eat an oreo

17

18 of 37

Repeated Actions

if there are oreos:

# eat an oreo

# because no one can eat just one

while there are oreos:

# eat an oreo

18

19 of 37

Repeated Actions

if there are oreos:

# eat an oreo

# because no one can eat just one

while there are oreos:

# eat an oreo

19

20 of 37

Repeated Actions

if there are oreos:

# eat an oreo

# because no one can eat just one

while there are oreos:

# eat an oreo

20

21 of 37

Repeated Actions

if there are oreos:

# eat an oreo

# because no one can eat just one

while there are oreos:

# eat an oreo

21

22 of 37

...until they’re gone :(

22

23 of 37

While Loops

while some condition is True:

# execute code block

Pseudocode

23

When you want to repeat an action while a condition is true

24 of 37

While Loop Syntax

24

while condition:

statement 1

statement 2

...

CONDITIONBoolean 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.

25 of 37

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

26 of 37

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

27 of 37

How do I make it print 10 times and then stop?

To do this, you have to answer two questions:

  • How do we figure out how many times the sample has printed?
  • How do we tell the interpreter to break out of the loop when it’s printed 10 times?

27

Demo: 01_while_simple.py

28 of 37

Using variables to track state

  • In computer science, the state of a program refers to its current values or contents
  • If we want to know how many times something has happened, we have to use a variable to track the state of the program
  • Each time the loop executes, we can increment the variable so that we know how long it’s been iterating

28

29 of 37

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

30 of 37

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

31 of 37

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.

32 of 37

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

  • On each page list the phone number to call at the top of the page.
  • Then include the script for that call
  • The next page is the same thing but with a different phone number on top.

Style 2

  • First page, put the script with blanks where you'll fill in details.
  • Next pages just list of phone numbers.

33 of 37

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

34 of 37

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])

35 of 37

For Loops

for each thing in a sequence:

# do some stuff

Pseudocode

35

When you want to go through a sequence of data…

36 of 37

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

37 of 37

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)