Studio 3 โ€” Loops & Iteration

ENGR 103 โ€” Week 3

๐Ÿ“‹ Contents

Studio Overview0:00โ€“0:10 โ€” Warm-Up0:10โ€“0:35 โ€” Part 1 โ€” Codedex Warmup (Guided)0:35โ€“1:10 โ€” Part 2 โ€” Practical Lab: Continuous Bioreactor Monitor1:10โ€“1:25 โ€” Part 3 โ€” Extension: Nested Loops for Sensor Grid1:25โ€“1:30 โ€” Wrap-Up & TA Check-Off

Student Instructions | Week 3 | Topics: while loops, for loops, range(), accumulators, break

Oregon State University | ENGR 103 | Dr. Cheng Li

Studio Overview

0:00โ€“0:10 โ€” Warm-Up

Predict the output of each snippet BEFORE running it:

Recall

Type and run this code:

# Snippet A
for i in range(3):
    print(i * 2)

# Snippet B
total = 0
for n in range(1, 6):
    total += n
print(total)
โœ๏ธ Your Task:

Write your predictions on paper first, then verify.

0:10โ€“0:35 โ€” Part 1 โ€” Codedex Warmup (Guided)

Type and run each snippet. Understand every line.

Exercise 3A โ€” while loop: Reactor Temperature Ramp

Type and run this code:

temp = 20.0   # starting temp ยฐC
step = 0

while temp <= 40.0:
    if temp >= 37.0:
        status = 'โš  WARNING'
    else:
        status = 'Normal'
    print(f'Step {step:3d}: {temp:.1f}ยฐC  [{status}]')
    temp += 1.5
    step += 1

print(f'Loop ended at {temp:.1f}ยฐC after {step} steps')
โœ๏ธ Your Task:

Change the step size to 2.0 and the warning threshold to 38.0. How many steps now?

Exercise 3B โ€” for loop with range: Growth Table

Type and run this code:

import math

N0  = 1000     # initial cells
mu  = 0.693    # growth rate (hโปยน), ~1 doubling/hr

print(f'{'Time(h)':>8}  {'N(cells)':>12}  {'Doublings':>10}')
print('-' * 38)
for t in range(0, 13, 2):   # 0 to 12 h, every 2 h
    N = N0 * math.exp(mu * t)
    doublings = t * mu / math.log(2)
    print(f'{t:>8d}  {N:>12.0f}  {doublings:>10.2f}')
โœ๏ธ Your Task:

Change mu to 0.35 (slower growth). How does the table change?

Exercise 3C โ€” break and accumulator

Type and run this code:

DO_readings = [8.2, 7.9, 7.5, 6.8, 5.9, 4.2, 3.1, 2.8, 5.5]
alarm_threshold = 4.0
total = 0
count = 0
alarm_triggered = False

for i, do in enumerate(DO_readings):
    total += do
    count += 1
    print(f'Reading {i+1}: DO = {do:.1f} mg/L')
    if do < alarm_threshold:
        print(f'  *** ALARM: DO below {alarm_threshold} mg/L ***')
        alarm_triggered = True
        break

print(f'\nAverage DO before alarm: {total/count:.2f} mg/L')
print(f'Alarm triggered: {alarm_triggered}')
โœ๏ธ Your Task:

What happens if no reading is below the threshold? Remove the break and rerun โ€” what changes?

0:35โ€“1:10 โ€” Part 2 โ€” Practical Lab: Continuous Bioreactor Monitor

Work in pairs. Simulate a bioreactor over 24 hours. Write a program that models hourly pH drop and triggers alerts.

Setup

Type and run this code:

# Bioreactor pH model
# pH drops 0.08 units/hour due to CO2 production
# Every 3 hours, operator adds buffer: pH increases by 0.20
# Start pH = 7.4, safe range: 6.8 to 7.8

pH = 7.4
ph_drop_per_hour = 0.08
buffer_add = 0.20
buffer_interval = 3   # hours
low_alarm  = 6.8
high_alarm = 7.8
โœ๏ธ Your Task:

Copy this setup block.

Tasks

โœ๏ธ Your Task:
1. Use a for loop over hours 1โ€“24.
2. Each hour: subtract the pH drop.
3. Every 3 hours (use %): add buffer and print a buffer event message.
4. Each hour: print the hour number, current pH, and status (OK / LOW ALARM / HIGH ALARM).
5. Track: total hours in alarm, minimum pH reached.
6. After the loop: print a summary (min pH, total alarm hours).

1:10โ€“1:25 โ€” Part 3 โ€” Extension: Nested Loops for Sensor Grid

Simulate a 3ร—3 grid of water quality sensors. Each cell stores a DO value. Print a grid report flagging any cell below 5.0 mg/L.

Extension

Type and run this code:

do_grid = [
    [8.2, 7.9, 8.1],
    [6.5, 3.8, 6.2],  # middle cell is LOW
    [7.8, 8.0, 7.6]
]
rows = ['North', 'Center', 'South']
cols = ['West', 'Center', 'East']

for i, row_name in enumerate(rows):
    for j, col_name in enumerate(cols):
        do = do_grid[i][j]
        flag = ' *** LOW ***' if do < 5.0 else ''
        print(f'{row_name:7s}-{col_name:7s}: DO={do:.1f}{flag}')
โœ๏ธ Your Task:

Run it and identify which cell is low. Then change the threshold to 7.0 and rerun.

1:25โ€“1:30 โ€” Wrap-Up & TA Check-Off

Show your TA the Part 2 bioreactor output. TA verifies: buffer events print correctly, alarms trigger at right pH values, summary is correct.