score_per_response from yesterday extracts all scores. But your advisor wants a quick sanity check — just the first response with a satisfaction score below a threshold, so they can inspect it before running the full analysis. A for loop would scan all 500 rows even if the answer is row 2. What's more efficient?
Stop as soon as I find it — like MATCH in Excel with exact match, but I can define my own stopping condition.
Exactly. A while loop with a counter gives you that control. while index < len(responses) keeps going as long as there are rows left. break stops immediately when the condition is met — no wasted iterations:
responses = [{"satisfaction": 4.2}, {"satisfaction": 1.8}, {"satisfaction": 3.5}]
index = 0
result = {}
while index < len(responses):
if float(responses[index]["satisfaction"]) < 2.5:
result = responses[index]
break
index += 1
print(result) # {'satisfaction': 1.8}Why not use a for loop and break out of that instead? Isn't for r in responses simpler?
You can — and for many cases it is simpler. The while loop here shows you the manual index counter pattern, which matters when you need fine control: skip items, jump positions, or check a condition that involves previous rows. The break behaviour is the same either way:
def find_first_low_score(responses: list, threshold: float) -> dict:
index = 0
while index < len(responses):
score = float(responses[index].get("satisfaction", 0))
if categorize_satisfaction(score, threshold, threshold + 1) == "low":
print(f"Found low score at index {index}: {score}")
return responses[index]
index += 1
return {}It returns the actual response dict — I can inspect every field, not just the score.
Row 2 of 500, found in two iterations. Your advisor gets the answer before the coffee cools.
I'm using categorize_satisfaction from Week 1 as the condition. Everything is composing now.
Always return a safe default when nothing is found. An empty dict {} is falsy — callers can test if result: to know whether anything matched. Returning None also works, but {} is consistent with the dict type your caller expects.
A while loop continues as long as its condition is True. Use break to exit early:
index = 0
while index < len(items):
if condition:
break # stop here
index += 1Return {} (empty dict) when nothing is found — it's falsy, so if result: works cleanly.
score_per_response from yesterday extracts all scores. But your advisor wants a quick sanity check — just the first response with a satisfaction score below a threshold, so they can inspect it before running the full analysis. A for loop would scan all 500 rows even if the answer is row 2. What's more efficient?
Stop as soon as I find it — like MATCH in Excel with exact match, but I can define my own stopping condition.
Exactly. A while loop with a counter gives you that control. while index < len(responses) keeps going as long as there are rows left. break stops immediately when the condition is met — no wasted iterations:
responses = [{"satisfaction": 4.2}, {"satisfaction": 1.8}, {"satisfaction": 3.5}]
index = 0
result = {}
while index < len(responses):
if float(responses[index]["satisfaction"]) < 2.5:
result = responses[index]
break
index += 1
print(result) # {'satisfaction': 1.8}Why not use a for loop and break out of that instead? Isn't for r in responses simpler?
You can — and for many cases it is simpler. The while loop here shows you the manual index counter pattern, which matters when you need fine control: skip items, jump positions, or check a condition that involves previous rows. The break behaviour is the same either way:
def find_first_low_score(responses: list, threshold: float) -> dict:
index = 0
while index < len(responses):
score = float(responses[index].get("satisfaction", 0))
if categorize_satisfaction(score, threshold, threshold + 1) == "low":
print(f"Found low score at index {index}: {score}")
return responses[index]
index += 1
return {}It returns the actual response dict — I can inspect every field, not just the score.
Row 2 of 500, found in two iterations. Your advisor gets the answer before the coffee cools.
I'm using categorize_satisfaction from Week 1 as the condition. Everything is composing now.
Always return a safe default when nothing is found. An empty dict {} is falsy — callers can test if result: to know whether anything matched. Returning None also works, but {} is consistent with the dict type your caller expects.
A while loop continues as long as its condition is True. Use break to exit early:
index = 0
while index < len(items):
if condition:
break # stop here
index += 1Return {} (empty dict) when nothing is found — it's falsy, so if result: works cleanly.
You need to quickly identify the first suspicious response in your dataset — the first one with a satisfaction score below a given threshold — so your advisor can inspect it before the full analysis runs. Write `find_first_low_score(responses, threshold)` that uses a while loop and break to return the first response dict with satisfaction below threshold, or `{}` if none found.
Tap each step for scaffolded hints.
No blank-editor panic.