You can compute ARPU per customer with compute_arpu_per_customer. Now imagine your board wants to know which customer on the Pro plan churned first this month. How do you find that?
In Excel I'd use VLOOKUP or an INDEX/MATCH to find the first row where plan=Pro and status=churned. It's always three formula levels deep.
In Python, a while loop with an index counter walks the list until you find the match, then break stops the search. No nested formulas. Just an index that increments:
i = 0
while i < len(customers):
if customers[i]["plan"] == plan and is_churned(...):
break
i += 1What happens if no customer on that plan is churned? Does the loop just run forever?
No — i < len(customers) is the guard. When i reaches the end without a match, the while condition becomes False and the loop exits naturally. Then you check if i reached the end — if so, return an empty dict. Here's the full function:
def find_first_churned(customers: list, plan: str) -> dict:
i = 0
while i < len(customers):
c = customers[i]
if c.get("plan") == plan and (c.get("status") == "churned" or c.get("mrr", 0) == 0):
print(f"Found churned customer at index {i}")
return c
i += 1
print("No churned customer found")
return {}So once I find the match, break — or here, return c — stops the search immediately. I'm not wasting time checking the rest.
Early exit is one of Python's best features. INDEX/MATCH always scans the whole column. Python stops at the first hit.
I just replaced INDEX/MATCH with a while loop that stops early. That's better in every way.
continue skips the current iteration without stopping the loop. break stops it entirely. Use break when you've found what you need — continue is for skipping invalid items while still searching.
A while loop runs as long as its condition is True.
i = 0
while i < len(items): # guard against over-run
if condition:
break # stop immediately
i += 1 # advance the counter| Keyword | Effect |
|---|---|
break | Exit the loop immediately |
continue | Skip this iteration, keep looping |
i += 1 | Advance the counter (or loop runs forever) |
Forget i += 1 and the loop runs forever. Always increment the counter on every non-break path.
You can compute ARPU per customer with compute_arpu_per_customer. Now imagine your board wants to know which customer on the Pro plan churned first this month. How do you find that?
In Excel I'd use VLOOKUP or an INDEX/MATCH to find the first row where plan=Pro and status=churned. It's always three formula levels deep.
In Python, a while loop with an index counter walks the list until you find the match, then break stops the search. No nested formulas. Just an index that increments:
i = 0
while i < len(customers):
if customers[i]["plan"] == plan and is_churned(...):
break
i += 1What happens if no customer on that plan is churned? Does the loop just run forever?
No — i < len(customers) is the guard. When i reaches the end without a match, the while condition becomes False and the loop exits naturally. Then you check if i reached the end — if so, return an empty dict. Here's the full function:
def find_first_churned(customers: list, plan: str) -> dict:
i = 0
while i < len(customers):
c = customers[i]
if c.get("plan") == plan and (c.get("status") == "churned" or c.get("mrr", 0) == 0):
print(f"Found churned customer at index {i}")
return c
i += 1
print("No churned customer found")
return {}So once I find the match, break — or here, return c — stops the search immediately. I'm not wasting time checking the rest.
Early exit is one of Python's best features. INDEX/MATCH always scans the whole column. Python stops at the first hit.
I just replaced INDEX/MATCH with a while loop that stops early. That's better in every way.
continue skips the current iteration without stopping the loop. break stops it entirely. Use break when you've found what you need — continue is for skipping invalid items while still searching.
A while loop runs as long as its condition is True.
i = 0
while i < len(items): # guard against over-run
if condition:
break # stop immediately
i += 1 # advance the counter| Keyword | Effect |
|---|---|
break | Exit the loop immediately |
continue | Skip this iteration, keep looping |
i += 1 | Advance the counter (or loop runs forever) |
Forget i += 1 and the loop runs forever. Always increment the counter on every non-break path.
Ava needs to know which customer on a given plan churned first — her board wants to review the first Pro churn of the month. Write `find_first_churned(customers, plan)` that iterates customers with a while loop and returns the first customer dict where plan matches and status is 'churned' or mrr is 0 — return an empty dict `{}` if none found.
Tap each step for scaffolded hints.
No blank-editor panic.