Skip to content

Sep 26: While Loops and Random

Learning Objectives

After today's class, you should be able to:

  • Identify the three main components of a while loop.
  • Use the random module to generate random numbers.
  • Explain when to use a while loop versus a for loop.

Reminders

  • Read Week 7 – ideally:
    • 1st half before Friday
    • 2nd half before Monday
  • Submit HW 7 – ideally:
    • 1 and 2 before Friday
    • 3 and 4 before Monday

POGIL Activity

  • While Loops
    • If you are absent today, complete this activity at home
    • Bring your completed activity to class or office hours

Example Code

Model 1

i = 0
while i < 3:
    print("the number is", i)
    i = i + 1
print("goodbye")

Question 9

total_count.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
total = 0
count = 0

n = None
while n != 0:
    n = float(input("Next number: "))
    total += n
    count += 1

print()
print("Total:", total)
print("Count:", count)