0% found this document useful (0 votes)
14 views

Week3 Lecture2

Uploaded by

frankhao4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Week3 Lecture2

Uploaded by

frankhao4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

while loops.

Week 3 | Lecture 2 (3.2)


Looping (Iterating)
§ Looping means repeating
something over and over
until a particular condition
is satisfied.
§ Looping (aka iteration) is
the second key control
structure in programming
(if-statements/branching
was the first).
Looping (Iterating)
List of
Customers
§ Looping means repeating Looping
something over and over
until a particular condition
is satisfied.

Send
Email Promotional
Email
Looping (Iterating)
List of
Tweets
§ Looping means repeating Looping
something over and over
until a particular condition
is satisfied.

Does the
Tweet
Yes/No
contain
#cleancode
Looping (Iterating)
§ Looping means repeating
something over and over
until a particular condition
is satisfied.

Check
for Looping
Yes/No
Waldo
Looping (Iterating)
§ Looping means repeating
something over and over
until a particular condition
is satisfied.

Check
for Looping
Yes/No
Waldo
Looping (Iterating)
§ Looping means repeating
something over and over
until a particular condition
is satisfied.

Check
for Looping
Yes/No
Waldo
Looping (Iterating)
§ Looping means repeating
something over and over
until a particular condition
is satisfied.

Check
for Looping
Yes/No
Waldo
Looping (Iterating)
§ Looping means repeating
something over and over
until a particular condition
is satisfied.

Check
for Looping
Yes/No
Waldo
Our programming branches so far…

statements statements

statements if statement while statement


statements
condition condition
statements True True

statements
statements False False

statements statements
While Loops
§ Sometimes we need to keep looping as
long as some condition is True, and stop
when it becomes False. Open your
§ Let's say you want to ask the user a
question. notebook
§ "Do you think the Toronto Maple Leafs will
win the Stanley Cup in your lifetime?"
Click Link:
§ If the user answers 'y', print out "You are
going to live for a very long time." If the 1. Asking the User a
user answers 'n', print out "Well, Question
sometimes miracles happen."
While Loops
§ Oh code kinda worked but if the user makes a typo, they
can’t participate in the questionnaire.
§ The general solution is to loop: to execute the same lines
of code more than once. This is also called iteration.
§ We're going to talk about one loop construct today: the
while-loop where you loop while some boolean expression
is True.
Must evaluate to
While Loops True or False Colon

§ The while loop keeps executing a while expression:


piece of code as long as a particular
condition is True. do something.
§ There must be a colon (:) at the end
of the while statement.
Indent
§ The action to be performed must
be indented.
While Loops
statements
§ The condition that gets evaluated is
just a boolean expression.
§ In particular it can include: False
§ Something that evaluates to True or Condition
False.
§ logical operators (and, or, not)
§ comparison operators True
§ function calls
Do
§ ... really anything that evaluates to Exit
Something
True or False.
Loop
Must evaluate to
While Loops True or False Colon

while expression:
do something.
Open your
notebook Indent

Click Link:
2. While Loops
while & for Loops
§ In Python there are two types for item in iterable:
of loops for and while.
do something.
§ For loops will be introduced
in Week 6.
§ What is the difference
between for loops and
while loops and when
would we use one over the
while expression:
other? do something.
while & for Loops
§ for loop for item in iterable:
§ The number of iterations to do something.
be done is already known.

cats = [‘Persian’, ‘Siamese’, ‘Ragdoll’]

for cat in cats:


print(cat) while expression:
do something.
>>> Persian
>>> Siamese
>>> Ragdoll
while & for Loops
§ while loop for item in iterable:
§ The number of iterations to be do something.
done is NOT known and iteration
continues until a condition is met.
x = 0
while x*x < 200:
print(x)
x += 1 I don’t know how
while expression:
many times I should do something.
>>> 0 iterate but I know
>>> … when I should stop.
>>> 14
while & for Loops
List of
Customers
§ for loop or while loop? Looping

Send
Email Promotional
Email
while & for Loops
List of
Tweets
§ for loop or while loop? Looping

Does the
Tweet
Yes/No
contain
#cleancode
while & for Loops
§ for loop or while loop?

Check
for Looping
Yes/No
Waldo
Infinite Loops x = 0
while x < 10:
§ Remember that a while loop ends when print(x)
the condition is False. x += 1
§ A common error when working with
while loops is for the condition to never True
be satisfied and therefore, the loop to x = 0, 1, 2,
continue forever (till infinity).
3, 4, 5, 6,
§ We need some way inside the loop for 7, 8, 9
the condition to become false.

False
x = 10
Infinite Loops
§ Remember that a while loop ends when
the condition is satisfied (True).
§ A common error when working with
while loops is for the condition to never
Open your
be satisfied and therefore, the loop to
continue forever (till infinity).
notebook
§ We need some way inside the loop for
the condition to become false. Click Link:
3. Infinite Loops
Variable Scope and Loops
x (Global)

def func(x): x = 0
x += 1 while x < 10:
x += 1
x (Local)
x = 0
func(x) x (Global)

x (Global)
While Loops
§ Let’s revisit our User Input code
and see if the While Loop will
solve out problem. Open your
notebook

Click Link:
4. Back to User Input
Breakout Session 1
§ Write code to print all the
numbers from 0 to 20 that aren’t
evenly divisible by either 3 or 5. Open your
§ Zero is divisible by everything and
should not appear in the output. notebook

Click Link:
5. Breakout Session 1
Turtles and while loops
§ I’m a little turtle and I want to take
steps to the right until I get to the
brick wall. Open your
§ However, I don’t know how far
away the brick wall I. notebook

Click Link:
6. Turtles and while
loops
Random Module
§ This module implements pseudo-
random number generators for
various distributions. Open your
import random notebook
random.uniform()
Click Link:
random.random()
7. Random Module
random.randint()

Guessing Game
§ Let’s build a simple guessing game.
§ Get the computer to choose a
random integer from 0 to 100.
§ Ask the user for a guess and allow the
user to input a guess or "q“.
§ If the user inputs "q" print a nice
message and end the program.
§ If the user enters a guess, tell them if
they should guess higher, lower, or if
they got it right.
§ If they got it right, print a nice
message and quit.
Guessing Game
§ Get the computer to choose a random integer
from 0 to 100.
§ The computer selects 45.

45

0 100
Guessing Game
§ The user guesses 64.
§ The computer says LOWER.

Guess
45 64

0 100
Guessing Game

45

0 64
Guessing Game
§ The user guesses 40.
§ The computer says HIGHER.

Guess
40 45

0 64
Guessing Game

45

40 64
Guessing Game
§ The user guesses 45.
§ The computer says YOU WIN.

Guess
45

40 64
Guessing Game
§ Let’s build a simple guessing game.
1. Get the computer to choose a
random integer from 0 to 100.
2. Ask the user for a guess and allow
Open your
the user to input a guess or "q“. notebook
3. If the user inputs "q" print a nice
message and end the program.
4. If the user enters a guess, tell them Click Link:
if they should guess higher, lower, 8. A Simple Guessing
or if they got it right.
Game
5. If they got it right, print a nice
message and quit.
Lecture Recap Practice!
§Looping (aka iteration) is the second key control
structure in programming (if-statements/branching
was the first).
§The basic idea of loops is to repeated execute the
same block code.
§Looping is very powerful idea.
§While loops is one of two loop types in Python.
while loops.
Week 3 | Lecture 2 (3.2)

You might also like