while
LoopsYou can connect to Gradescope to take weekly graded quiz today during the last 15 minutes of the class.
Once you start the quiz you have 15 minutes to finish it.
You can only take this quiz today.
There is not makeup for the weekly quiz because Gradescope does not permit it.
If you have the textbook you should read Chapter 4, Repetition Structures, from Starting Out with Python.
I have posted a solution to homework 3 here.
Let's take a look.
I have posted homework 5 here.
It is due this coming Sunday at 11:59 PM.
Are there any questions before I begin?
>>> a + 1
File "<stdin>", line 1
a + 1
^
IndentationError: unexpected indent
>>> a + 1 6
round
function ...
>>> fahrenheit = 41 >>> (fahrenheit - 32) / 1.8 5.0
>>> fahrenheit = 41 >>> (fahrenheit - 32) // 1.8 4.0
int
conversion functionint
we get
>>> fahrenheit = 35 >>> int((fahrenheit - 32) / 1.8) 1
round
we get
>>>> round((fahrenheit - 32) / 1.8) 2
>>> (fahrenheit - 32) / 1.8 1.6666666666666665
name=input("Please enter your name: ")
name = input("Please enter your name: ")
and
returns True
if both its
operands
are True
>>> 5 > 4 and 4 > 2 True
False
, and
returns
False
>>> 5 > 4 and 4 < 2 False
False
if both its operands are False
>>> 5 < 4 and 4 < 2 False
or
returns True
if either of its operands are
True
>>> 5 > 4 or 4 < 2 True
False
if both of its operands are
False
>>> 5 < 4 or 4 < 2 False
not
operator takes only one operandTrue
becomes False
>>> not 5 > 4 False
False
becomes True
>>> not 5 < 4 True
and
p | q | p and q |
---|---|---|
True |
True |
True |
True |
False |
False |
False |
True |
False |
False |
False |
False |
or
p | q | p or q |
---|---|---|
True |
True |
True |
True |
False |
True |
False |
True |
True |
False |
False |
False |
not
p | not p |
---|---|
True |
False |
False |
True |
a > b and c > d
False
...False
...True
or
False
a > b or c > d
True
...True
...True
or
False
** | Exponentiation |
* / // % | Multiplication, division and remainder |
+ - | Addition and subtraction |
> < >= <= == != | Relational Operators |
not | Logical NOT |
and | Logical AND |
or | Logical OR |
and
operator
age >= 16 and age <= 65:
or
operator
age < 16 or age > 65:
True
or False
minor = age < 10 senior = age > 65
while
Loopswhile
loopwhile
loop keeps repeating as long as a certain condition is
True
while BOOLEAN_EXPRESSION:
STATEMENT
STATEMENT
...
False
$ cat question.py # keeps asking a question until it gets the right answer reply = "" while reply != "yes" : reply = input("Are we there yet? ") print("Yeah!") $ python3 question.py Are we there yet? no Are we there yet? no Are we there yet? no Are we there yet? yes Yeah!
while
loopwhile
loop works
$ cat total.py # add a series of numbers entered by the user # demonstrates the use of a while loop done = "no" total = 0 while done != "yes": number = int(input("Please enter a number: ")) total = total + number done = input("If you are finished, enter \"yes\" ") print("Your total is", total) $ python3 total.py Please enter a number: 4 If you are finished, enter "yes" Please enter a number: 5 If you are finished, enter "yes" Please enter a number: 6 If you are finished, enter "yes" Please enter a number: 7 If you are finished, enter "yes" Please enter a number: 8 If you are finished, enter "yes" Please enter a number: 9 If you are finished, enter "yes" Please enter a number: 10 If you are finished, enter "yes" yes Your total is 49
while
loop is evaluated before entering
the loop
while
loop ...$ cat total_bad.py # tries to add a series of numbers entered by the user # but cannot enter the loop because done is improperly initialized total = 0 while done != "yes": number = int(input("Please enter a number: ")) total = total + number done = input("If you are finished, enter \"yes\" ") print("Your total is", total)
$ python3 total_bad.py
Traceback (most recent call last):
File "total_bad.py", line 5, in <module>
while done != "yes":
NameError: name 'done' is not defined
$ cat total_stupid.py # tries to add a series of numbers entered by the user # but can't because done is given the wrong initial value done = "yes" total = 0 while done != "yes": number = int(input("Please enter a number: ")) total = total + number done = input("If you are finished, enter \"yes\" ") print("Your total is", total)
$ python3 total_stupid.py Your total is 0
while
loop is False
...while
loop stops when its condition becomes False
while
header has to be True
...False
...
>>> done = False
>>> while not done:
print("Are we there yet?")
Are we there yet?
Are we there yet?
Are we there yet?
Are we there yet?
Traceback (most recent call last):
^C File "<stdin>", line 2, in <module>
KeyboardInterrupt
Garbage in Garbage out
$23.94you enter
$2394
while
loops are often used to check values entered by the user
get value from user
while value is not greater than 0:
print error message
get a new value from the user
$ cat validation.py # this program asks the user to enter an # integer greater than 0 and keeps asking # until it gets a proper value # it is an example of using a while loop # for data validation value = int(input("Please enter an integer greater than 0: ")) while value <= 0: print(value, "is not greater than 0") value = int(input("Please enter an integer greater than 0: ")) print(value, "is greater than 0") $ python3 validation.py Please enter an integer greater than 0: -3 -3 is not greater than 0 Please enter an integer greater than 0: 0 0 is not greater than 0 Please enter an integer greater than 0: 5 5 is greater than 0
input
statement ...while
loopFalse
True
True
if the input is good
set a flag to False
while the flag is not True
get input from the user and store in a variable
if the input value is good
set the flag to True
else:
print an error message
done = False while not done: value = int(input("Please enter an integer greater than 0: ")) if value > 0: done = True else: print(value, "is not greater than 0") print(value, "is greater than 0")
$ python3 validation2.py Please enter an integer greater than 0: -1 -1 is not greater than 0 Please enter an integer greater than 0: 0 0 is not greater than 0 Please enter an integer greater than 0: 5 5 is greater than 0
bmi = weight / height ** 2 * 703
while
loop ...$ cat bmi.py # calculates the Body Mass Index using validation loops done = False while not done: height = int(input("Please enter your height in inches: ")) if height <= 0 or height >= 107: print("Height must be greater than 0 and less than or equal to 107") else: done = True print() done = False while not done: weight = int(input("Please enter your weight in pounds: ")) if weight <= 0 or weight >= 975: print("Weight must be greater than 9 and less than or equal to 975") else: done = True print() bmi = weight / height ** 2 * 703 print("Your Body Mass Index is", round(bmi)) bmi = weight / height ** 2 * 703 print("Your Body Mass Index is", round(bmi)) $ python3 bmi.py Please enter your height in inches: 0 Height must be greater than 0 and less than or equal to 107 Please enter your height in inches: 200 Height must be greater than 0 and less than or equal to 107 Please enter your height in inches: 60 Please enter your weight in pounds: 1000 Weight must be greater than 9 and less than or equal to 975 Please enter your weight in pounds: 0 Weight must be greater than 9 and less than or equal to 975 Please enter your weight in pounds: 150 Your Body Mass Index is 29
while
loopdistance = speed_of_vehicle * time_traveled_today \ + distance_of_starting_point