1 of 46

While Loops

Jake Shoudy

August 24, 2022

CSCI 110 - Lecture 7

2 of 46

Today’s Agenda

  • Recap
  • While loops
  • Practice!

3 of 46

Announcements

4 of 46

Pre-class Surveys

Two surveys posted on Ed

Please take them 🙏 🙏 🙏

5 of 46

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

6 of 46

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.

7 of 46

Reminder: Project 1: Part 1

  • Blackjack!
  • 3 files:
    • Name: take a card # and output the card name
    • Value: take a card # and output the card value
    • End Status: take a hand value and output BLACKJACK or BUST
  • Released in Ed
  • Due on Sunday at 11:59pm

8 of 46

HW2 Posted!

  • Covers conditionals, boolean logic, and has one problem on while loops (today’s class)
  • Due next Wednesday (8/31) at 11:59pm

9 of 46

Quiz 2 on Friday!

  • MEET IN THE LAB DOWNSTAIRS!!
  • BE ON TIME! From now on quizzes will start EXACTLY at :05 and end at :20
  • Don’t forget to submit each question! You can always change the answer and resubmit.
  • Make sure you are logged in to YOUR account
  • If you score higher than you did on quiz 1, we will replace your quiz 1 score.

10 of 46

TA Office Hours

  • Aminu
    • Mondays from 5-6pm (Library 3rd floor)
  • Aman
    • Mondays from 10:30-11:30am (Library 3rd floor)
  • Angel:
    • Tuesdays from 3-4pm (Library 3rd floor)
  • Arun:
    • Fridays from 9:30-10:30am (Library 3rd floor)

All available on class calendar

11 of 46

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!

12 of 46

Feedback Form

Anonymous feedback form

Share with me! Also posted in Ed

13 of 46

Recap

14 of 46

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

15 of 46

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

16 of 46

Review: Logical Operators

answer = input("What city is Fisk in?")

if answer == "nashville" or "Nashville":

print("Correct!")

Find the bug!

17 of 46

Review: Logical Operators

answer = input("What city is Fisk in?")

if answer == "nashville" or "Nashville":

print("Correct!")

Evaluates this first

18 of 46

Review: Logical Operators

answer = input("What city is Fisk in?")

if answer == "nashville" or "Nashville":

print("Correct!")

then evaluates this???

19 of 46

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

20 of 46

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

21 of 46

Review: Logical Operators

answer = input("What city is Fisk in?")

if answer == "nashville" or answer == "Nashville":

print("Correct!")

THIS IS RIGHT

22 of 46

While Loops

23 of 46

Program: Password

Input: user input

Output:

prints “Access granted!” if user input equals “helloworld”, otherwise prints “Wrong password.”

24 of 46

Program: Password

password = input('Password?')

if password == “helloworld”:

print('Access granted!')

else:

print('Wrong password.')

You only get one try!

25 of 46

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

26 of 46

Program: Password

password = input('Password?')

if password == 'helloworld':

print('Access granted!')

else:

print('Wrong password.')

27 of 46

Program: Password

password = ''

while password != 'helloworld':

password = input('Password?')

if password == 'helloworld':

print('Access granted!')

else:

print('Wrong password.')

28 of 46

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

29 of 46

Program: Password

password = ''

while password != 'helloworld':

password = input('Password?')

if password == 'helloworld':

print('Access granted!')

else:

print('Wrong password.')

keep asking for the password

30 of 46

Program: Password

password = ''

while password != 'helloworld':

password = input('Password?')

if password == 'helloworld':

print('Access granted!')

else:

print('Wrong password.')

need an initial value

31 of 46

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.

32 of 46

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

33 of 46

while [expression evaluates True]:

[do stuff]

Syntax

34 of 46

Let’s Code!

35 of 46

while [expression evaluates True]:

[do stuff]

This expression needs to evaluate to True to run, BUT...

Syntax

36 of 46

while [expression evaluates True]:

[do stuff]

...also needs to be able to evaluate to False.

Syntax

37 of 46

What happens?

count = 3

while count > 0:

print(count)

count = count - 1

print("Blast off!")

38 of 46

What happens? Why?

x = 1

while x < 1:

print("test")

39 of 46

What happens? Why?

x = 1

while x < 2:

print("test")

40 of 46

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

41 of 46

Will this print? Why?

x = 1

while x < 2:

print("test")

print("will this print?")

How to fix it?

42 of 46

What happens? Why?

x = 1

while x < 5:

print("test")

x = x + 1

43 of 46

What happens? Why?

x = 1

while x < 5:

print("test")

x = x + 1

print("test")

44 of 46

What happens? Why?

x = 1

while x < 5:

print("test")

45 of 46

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

46 of 46

Questions?