Student Instructions | Week 2 | Topics: Strings, f-strings, input(), if/elif/else
Oregon State University | ENGR 103 | Dr. Cheng Li
Quick recall: open Spyder, type and run the following without looking at notes:
Type and run this code:
x = 3.7
y = '8.2'
print(type(x), type(y))
print(x + float(y))What will print? Run it and check.
Type and run each snippet. Understand what every line does.
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])What is fields[3]? What does fields[1::2] give you?
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')Add a line that prints BOD = 245.8 mg/L with exactly 1 decimal place.
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}')Test with values: 9.0, 6.5, 3.8, 1.5, 0.0
Work in pairs. Build an interactive water quality checker. The program asks the user for readings and prints a formatted instrument-style report.
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)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 ====================================
Match this format as closely as possible.
Modify your report to also print a list of which parameters failed and suggest corrective actions.
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}')Add this logic to your existing script.
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).