CS 104:
Introduction to Computers and Ethics
While Loops
Announcements & Reminders
Announcements & Reminders
# I didn’t know what return does, but I have since learned that it does…x, y, z
Announcements & Reminders
Extra help with functions
Review: Logical Operators
What are they?
Operators that combine booleans
and: True only if both operands are True
or: True if either (or both) operand is True
not: Inverts value of its operand
order of operations: PNAO
Why are they important?
Enable programs to make decisions based on multiple conditions
Review: Logical Operators
not price >= 50
price = 75
False
price == 70 or price == 75
price > 20 and price < 40
price != 50 and not price <= 60
price == 5 or (price > 10 and price < 80)
True
False
True
True
Program: Password
Input: user input
Output:
prints “Access granted!” if user input equals “helloworld”, otherwise prints “Wrong password.”
Program: Password
password = input('Password?')
if password == “helloworld”:
print('Access granted!')
else:
print('Wrong password.')
You only get one try!
Program: Password
password = input('Password?')
if password == “helloworld”:
print('Access granted!')
else:
print('Wrong password.')
Should take new input and check it until the user gets it right
While Loops
Program: Password
password = input('Password?')
if password == 'helloworld':
print('Access granted!')
else:
print('Wrong password.')
Program: Password
password = ''
while password != 'helloworld':
password = input('Password?')
if password == 'helloworld':
print('Access granted!')
else:
print('Wrong password.')
Program: Password
password = ''
while password != 'helloworld':
password = input('Password?')
if password == 'helloworld':
print('Access granted!')
else:
print('Wrong password.')
while the password isn't correct
Program: Password
password = ''
while password != 'helloworld':
password = input('Password?')
if password == 'helloworld':
print('Access granted!')
else:
print('Wrong password.')
keep asking for the password
Program: Password
password = ''
while password != 'helloworld':
password = input('Password?')
if password == 'helloworld':
print('Access granted!')
else:
print('Wrong password.')
need an initial value
while loops
Control structure that runs a block of code repeatedly until a certain condition is no longer met
while condition is true:
do something
while [expression evaluates True]:
[do stuff]
Syntax
indented!
while [expression evaluates True]:
[do stuff]
This expression needs to evaluate to True to run, BUT...
Syntax
while [expression evaluates True]:
[do stuff]
...also needs to be able to evaluate to False.
Syntax
What happens?
count = 3
while count > 0:
print(count)
count = count - 1
print("Blast off!")
Will this print? Why?
x = 1
while x < 5:
print("test")
print("will this print?")
Infinite loops!
while True:
print("Still true")
print("This will never print")
Make sure condition includes a variable that changes in the loop body so condition eventually becomes False!
What happens? Why?
x = 1
while x < 5:
print("test")
x = x + 1
What happens? Why?
x = 1
while x < 5:
print("test")
x = x + 1
print("will this print")
While Loops + Strings
Let's write a while loop that iterates through each of the letters in the string ‘hello’, and prints each letter out
word = 'hello'
index = 0
while index < len(word):
print(word[index])
index += 1
EdStem Practice
Review: While Loops
What are they?
Control structure that runs a block of code repeatedly until a certain condition is no longer met
Why are they important?
Write code once and run many times - do not need to copy and paste same code over and over