While Loops
Jake Shoudy
August 24, 2022
CSCI 110 - Lecture 7
Today’s Agenda
|
|
| |
| |
| |
| |
Announcements
Pre-class Surveys
Two surveys posted on Ed
Please take them 🙏 🙏 🙏
Google TechExchange
What: Google program for HBCU students to learn CS things on Google campus (targeted at sophomores)
Google program leads will be on Fisk Campus to talk about it!
Timing: Aug 29th 2022, 5-7pm See more here
Reminder: HW1
Due tonight at 11:59pm
Only your most recent submission will be graded. If you submit after the deadline a late day will be used.
Reminder: Project 1: Part 1
HW2 Posted!
Quiz 2 on Friday!
TA Office Hours
All available on class calendar
Google 20% TAs
Missed the first 4 lectures of class? Just plain lost?
Some folks from Google are volunteering their time to help you with your computer science scoolwork!
If you feel that you are falling behind and want more 1:1 attention let me know and I will try to pair you with someone who can help!
Feedback Form
Recap
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
Review: Logical Operators
answer = input("What city is Fisk in?")
if answer == "nashville" or "Nashville":
print("Correct!")
Find the bug!
Review: Logical Operators
answer = input("What city is Fisk in?")
if answer == "nashville" or "Nashville":
print("Correct!")
Evaluates this first
Review: Logical Operators
answer = input("What city is Fisk in?")
if answer == "nashville" or "Nashville":
print("Correct!")
then evaluates this???
Review: Logical Operators
answer = input("What city is Fisk in?")
if answer == "nashville" or "Nashville":
print("Correct!")
Python considers "" (empty string) to be FALSE
Python considers all other strings to be TRUE
Review: Logical Operators
answer = input("What city is Fisk in?")
if answer == "nashville" or "Nashville":
print("Correct!")
Because OR only needs one condition to be True,
this entire statement will always be True
Review: Logical Operators
answer = input("What city is Fisk in?")
if answer == "nashville" or answer == "Nashville":
print("Correct!")
THIS IS RIGHT
While Loops
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
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
if vs. while
If the lights are on, shout "LIGHTS!".
While the lights are on, shout "LIGHTS!".
If you've clapped your hands less than 5 times, clap your hands 1 time
While you've clapped your hands less than 5 times, clap your hands 1 time
If you like tacos, give me a thumbs up.
While you like tacos, give me a thumbs up.
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
Let’s Code!
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!")
What happens? Why?
x = 1
while x < 1:
print("test")
What happens? Why?
x = 1
while x < 2:
print("test")
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
Will this print? Why?
x = 1
while x < 2:
print("test")
print("will this print?")
How to fix it?
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("test")
What happens? Why?
x = 1
while x < 5:
print("test")
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
Questions?