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

week4_lecture2

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

week4_lecture2

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

more while loops.

Week 4 | Lecture 2 (4.2)


This Week’s Content
▪ Lecture 4.1
▪ function review, while loops
▪ Reading: Chapter 9
▪ Lecture 4.2
▪ more loops
▪ Reading: Chapter 9
▪ Lecture 4.3
▪ Midterm Review
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
▪ The condition that gets evaluated is Do
just an boolean expression.
Something
▪ In particular it can include: while
▪ Something that evaluates to True or Condition
False.
▪ logical operators (and, or, not) True
▪ comparison operators
▪ function calls
▪ ... really anything that evaluates to Exit
True or False. False
Loop
Do
Something
True
if Condition while Condition
True

Do
Something
False False
Refresher

▪How many printouts will


the following while loop
produce? Open your
notebook
x = 1
while x < 4: Click Link:
print(x) 1. Refresher
x = x + 1
Refresher

▪Just like for if-statements, if


you use and or or in a while-
loop expression, it is subject to Open your
lazy evaluation. notebook
▪Only if x < 4 is True will y < 4
be evaluated. #solazy Click Link:
2. Lazy Evaluation
while x < 4 and y < 4:
...
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:
3. Turtles and while
loops
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:
4. Breakout Session 1
Random Module
▪ This module implements pseudo-
random number generators for
various distributions. Open your
import random notebook
random.uniform()
Click Link:
random.random()
5. 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, 6. A Simple Guessing
or if they got it right.
Game
5. If they got it right, print a nice
message and quit.
Lecture Recap

▪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.
more while loops.
Week 4 | Lecture 2 (4.2)

You might also like