1 of 29

CS 104:

Introduction to Computers and Ethics

While Loops

2 of 29

Announcements & Reminders

  • Homework 5 due Friday, Sep 30
    • Can submit up to 3 days late for 10% off per day
  • Google STEP and SWE internship applications open!
    • All you need is a resume + unofficial transcript to apply
    • You should apply, even if you don’t feel you have enough experience yet!
    • More internships listed at cs104.org

3 of 29

Announcements & Reminders

  • Opportunity to get points back on your quiz
    • Will open up the quiz again Tuesday evening until Friday evening
    • You can re-submit coding questions (not multiple choice), but must include in a comment what you learned that you did not know at the time of the quiz

# I didn’t know what return does, but I have since learned that it does…x, y, z

    • Feel free to ask questions in office hours
    • If you missed the original quiz, cannot resubmit

4 of 29

Announcements & Reminders

Extra help with functions

  • Virtual instructional sessions to go over functions and give you time to ask questions on Monday and Tuesday this week 5-6 pm
    • If you cannot make either of these, I will post any code written in a workspace on EdStem!
  • Videos with solution walkthroughs posted for Homework 3 (1, 2, and 4 posted this week)

5 of 29

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

6 of 29

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

7 of 29

Program: Password

Input: user input

Output:

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

8 of 29

Program: Password

password = input('Password?')

if password == “helloworld”:

print('Access granted!')

else:

print('Wrong password.')

You only get one try!

9 of 29

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

10 of 29

While Loops

11 of 29

Program: Password

password = input('Password?')

if password == 'helloworld':

print('Access granted!')

else:

print('Wrong password.')

12 of 29

Program: Password

password = ''

while password != 'helloworld':

password = input('Password?')

if password == 'helloworld':

print('Access granted!')

else:

print('Wrong password.')

13 of 29

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

14 of 29

Program: Password

password = ''

while password != 'helloworld':

password = input('Password?')

if password == 'helloworld':

print('Access granted!')

else:

print('Wrong password.')

keep asking for the password

15 of 29

Program: Password

password = ''

while password != 'helloworld':

password = input('Password?')

if password == 'helloworld':

print('Access granted!')

else:

print('Wrong password.')

need an initial value

16 of 29

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

17 of 29

while [expression evaluates True]:

[do stuff]

Syntax

indented!

18 of 29

while [expression evaluates True]:

[do stuff]

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

Syntax

19 of 29

while [expression evaluates True]:

[do stuff]

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

Syntax

20 of 29

What happens?

count = 3

while count > 0:

print(count)

count = count - 1

print("Blast off!")

21 of 29

Will this print? Why?

x = 1

while x < 5:

print("test")

print("will this print?")

22 of 29

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!

23 of 29

What happens? Why?

x = 1

while x < 5:

print("test")

x = x + 1

24 of 29

What happens? Why?

x = 1

while x < 5:

print("test")

x = x + 1

print("will this print")

25 of 29

While Loops + Strings

26 of 29

Let's write a while loop that iterates through each of the letters in the string ‘hello’, and prints each letter out

27 of 29

word = 'hello'

index = 0

while index < len(word):

print(word[index])

index += 1

28 of 29

EdStem Practice

29 of 29

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