while Loopswhile LoopsAre there any questions before I begin?
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.
After I finish talking I will pass out the papers for today's graded quiz.
Today I will discuss while loops
and how they can be used.
>>> a + 1
File "<stdin>", line 1
a + 1
^
IndentationError: unexpected indent
>>> a + 1 6
input to ask the
user for an integer
input function asks the user for a valueint function on the value
returned by input
round function ...
>>> fahrenheit = 41 >>> (fahrenheit - 32) / 1.8 5.0
>>> fahrenheit = 41 >>> (fahrenheit - 32) // 1.8 4.0
int conversion functionint in calculating Celsius conversiton>>> 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 |
and only returns True if both values are Trueor
| p | q | p or q |
|---|---|---|
True |
True |
True |
True |
False |
True |
False |
True |
True |
False |
False |
False |
or only returns False if both values are Falesnot
| p | not p |
|---|---|
True |
False |
False |
True |
| ** | 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 loop whenever we do not know ...
secret_num = 7
guess = 0
tries = 0
while guess != secret_num:
guess = int(input("Your Guess: "))
if guess == secret_num:
print("Yes, the number is", secret_num)
else:
print(guess, "is not the number")
tries = tries + 1
print("You guessed correctly after", tries , "tries")
Your Guess: 4 4 is not the number Your Guess: 2 2 is not the number Your Guess: 5 5 is not the number Your Guess: 7 Yes, the number is 7 You guessed correctly after 3 tries
while Loopswhile 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: yes
Your total is 15
while loop is evaluated before entering
the loop
while loop ...while loopwhile loop ...False ...while loop that prints the numbers
from 1 to 5
num = 1
while num <= 5:
print(num)
num = num + 1
1 2 3 4 5
num = 1
while num <= 5:
print(num)
1 1 1 1 ...
Garbage in Garbage out
$23.94you enter
$2394
while loops are often used to check values entered by the user
get number from user and store in a variable
convert this into integer
while value is not greater than 0:
print error message
get a new value from the user
convert inot integer
$ 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 ...TrueTrue 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
convert value to integer
if the input value is valid
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 loopwhile Loop to Add Numbersinput statements ...False ...
False
True
if the value entered by the user is 0 or less
# script to add positive integers using a while loop
done = False
total = 0
while not done:
num = int(input("Number: "))
if num > 0:
total = total + num
else:
done = True
print("Total", total)
$ python3 while_add.py Number: 45 Number: 84 Number: 92 Number: 5 Number: 0 Total 226
distance = speed_of_vehicle * time_traveled_today \
+ distance_of_starting_point