Studio 2 โ€” Strings, Input/Output & Branching

ENGR 103 โ€” Week 2

๐Ÿ“‹ Contents

Studio Overview0:00โ€“0:10 โ€” Warm-Up Review0:10โ€“0:35 โ€” Part 1 โ€” Codedex Warmup (Guided)0:35โ€“1:10 โ€” Part 2 โ€” Practical Lab: Automated Water Quality Report1:10โ€“1:25 โ€” Part 3 โ€” Extension: Multi-Parameter Alert System1:25โ€“1:30 โ€” Wrap-Up & TA Check-Off

Student Instructions | Week 2 | Topics: Strings, f-strings, input(), if/elif/else

Oregon State University | ENGR 103 | Dr. Cheng Li

Studio Overview

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

Quick recall: open Spyder, type and run the following without looking at notes:

Recall Exercise

Type and run this code:

x = 3.7
y = '8.2'
print(type(x), type(y))
print(x + float(y))
โœ๏ธ Your Task:

What will print? Run it and check.

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

Type and run each snippet. Understand what every line does.

Exercise 2A โ€” String Operations

Type and run this code:

species = 'Geobacter sulfurreducens'
print(len(species))           # length
print(species[0])             # first char
print(species[-1])            # last char
print(species[:9])            # first 9 chars
print(species.upper())
print('Geobacter' in species) # membership

# Parsing a sensor data string
reading = 'pH,7.42,DO,8.15,Temp,22.3'
fields = reading.split(',')
print(fields)
print('pH value:', fields[1])
โœ๏ธ Your Task:

What is fields[3]? What does fields[1::2] give you?

Exercise 2B โ€” f-strings & Formatting

Type and run this code:

station = 'WQ-04'
pH      = 7.326
do      = 8.054
temp_C  = 23.7
temp_F  = temp_C * 9/5 + 32

print(f'Station:     {station}')
print(f'pH:          {pH:.2f}')
print(f'DO:          {do:.3f} mg/L')
print(f'Temperature: {temp_C:.1f}ยฐC / {temp_F:.1f}ยฐF')
print(f'Scientific:  {do:.3e} mg/L')
โœ๏ธ Your Task:

Add a line that prints BOD = 245.8 mg/L with exactly 1 decimal place.

Exercise 2C โ€” if/elif/else for Water Quality

Type and run this code:

DO = float(input('Enter dissolved oxygen (mg/L): '))

if DO >= 8.0:
    status = 'Excellent'
elif DO >= 6.0:
    status = 'Good'
elif DO >= 4.0:
    status = 'Moderate'
elif DO >= 2.0:
    status = 'Poor'
else:
    status = 'Hypoxic โ€” Critical'

print(f'DO = {DO:.2f} mg/L โ†’ Water Quality: {status}')
โœ๏ธ Your Task:

Test with values: 9.0, 6.5, 3.8, 1.5, 0.0

0:35โ€“1:10 โ€” Part 2 โ€” Practical Lab: Automated Water Quality Report

Work in pairs. Build an interactive water quality checker. The program asks the user for readings and prints a formatted instrument-style report.

Requirements

โœ๏ธ Your Task:
Your script must:
1. Prompt user for: sample ID (str), pH (float), DO (float), temperature ยฐC (float), BOD (float)
2. Classify overall water quality using ALL parameters:
   - pH: 6.5โ€“8.5 = OK, else FAIL
   - DO: โ‰ฅ 6.0 = OK, else FAIL
   - Temp: โ‰ค 25ยฐC = OK, else FAIL
   - BOD: โ‰ค 250 mg/L = OK, else FAIL
3. Print a formatted report (see sample output below)
4. Print overall status: PASS (all OK) or FAIL (any parameter fails)

Sample Output

Type and run this code:

==================================== WATER QUALITY REPORT โ€” Sample WQ-07 ==================================== pH: 7.32 [OK] DO: 5.80 [FAIL โ€” below 6.0 mg/L] Temperature: 26.1ยฐC [FAIL โ€” above 25ยฐC] BOD: 180.0 [OK] ------------------------------------ OVERALL STATUS: FAIL ====================================

โœ๏ธ Your Task:

Match this format as closely as possible.

1:10โ€“1:25 โ€” Part 3 โ€” Extension: Multi-Parameter Alert System

Modify your report to also print a list of which parameters failed and suggest corrective actions.

Extension Logic

Type and run this code:

# After computing ok flags, collect failures:
failures = []
if not pH_ok:
    failures.append(f'pH={pH:.2f} โ€” add buffer')
if not do_ok:
    failures.append(f'DO={DO:.2f} โ€” increase aeration')
if not temp_ok:
    failures.append(f'Temp={temp_C:.1f}ยฐC โ€” cooling needed')
if not bod_ok:
    failures.append(f'BOD={BOD:.0f} โ€” treatment required')

if failures:
    print('\nRequired Actions:')
    for f in failures:
        print(f'  โ†’ {f}')
โœ๏ธ Your Task:

Add this logic to your existing script.

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

Demo your Part 2 script to your TA using these test values: ID='WQ-99', pH=5.9, DO=7.2, Temp=24.0, BOD=310. Expected: FAIL (pH + BOD fail).