Student Instructions | Week 3 | Topics: while loops, for loops, range(), accumulators, break
Oregon State University | ENGR 103 | Dr. Cheng Li
Predict the output of each snippet BEFORE running it:
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)Write your predictions on paper first, then verify.
Type and run each snippet. Understand every line.
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')Change the step size to 2.0 and the warning threshold to 38.0. How many steps now?
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}')Change mu to 0.35 (slower growth). How does the table change?
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}')What happens if no reading is below the threshold? Remove the break and rerun โ what changes?
Work in pairs. Simulate a bioreactor over 24 hours. Write a program that models hourly pH drop and triggers alerts.
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.8Copy this setup block.
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).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.
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}')Run it and identify which cell is low. Then change the threshold to 7.0 and rerun.
Show your TA the Part 2 bioreactor output. TA verifies: buffer events print correctly, alarms trigger at right pH values, summary is correct.