You have spent = 150 and limit = 100. You want a clean yes-or-no answer to the question "is this over budget?" What type of value should the function return?
A boolean — True or False, nothing else.
Right. And Python comparison operators already produce booleans. 150 > 100 evaluates to True. 80 > 100 evaluates to False. You don't need if to wrap them — the comparison is the answer:
spent > limitReturn that expression directly from the function.
So I don't need if spent > limit: return True else: return False?
That works, but it's six words of ceremony wrapped around one boolean. Cleaner to just return the comparison:
def is_over_budget(spent, limit):
return spent > limitSame behavior, half the code.
What about the boundary? If spent == limit, am I over or not?
That's where > vs >= matters. 100 > 100 is False — strictly greater. 100 >= 100 is True — greater or equal. For "over budget," being exactly at the limit usually means you're not over yet — so > is the right choice. Make the boundary decision explicit.
So the whole function is one line — return the comparison. That's a pattern I can use anywhere I need a yes-no answer.
A one-line predicate is often the cleanest function you can write. No branches, no ceremony, just the question and its direct, boolean answer every time.
TL;DR: a comparison already returns a boolean — return it directly.
> — strictly greater than>= — greater than or equal< / <= — the mirror operators== — equal (two equals, not one)| Expression | spent=100, limit=100 |
|---|---|
spent > limit | False |
spent >= limit | True |
Pick the operator that matches your domain. "Over budget" usually means strictly over, not at-the-edge.
Write `is_over_budget(spent, limit)` that returns `True` when spent is strictly greater than limit, `False` otherwise. At exactly the limit, return `False`.
Tap each step for scaffolded hints.
No blank-editor panic.
You have spent = 150 and limit = 100. You want a clean yes-or-no answer to the question "is this over budget?" What type of value should the function return?
A boolean — True or False, nothing else.
Right. And Python comparison operators already produce booleans. 150 > 100 evaluates to True. 80 > 100 evaluates to False. You don't need if to wrap them — the comparison is the answer:
spent > limitReturn that expression directly from the function.
So I don't need if spent > limit: return True else: return False?
That works, but it's six words of ceremony wrapped around one boolean. Cleaner to just return the comparison:
def is_over_budget(spent, limit):
return spent > limitSame behavior, half the code.
What about the boundary? If spent == limit, am I over or not?
That's where > vs >= matters. 100 > 100 is False — strictly greater. 100 >= 100 is True — greater or equal. For "over budget," being exactly at the limit usually means you're not over yet — so > is the right choice. Make the boundary decision explicit.
So the whole function is one line — return the comparison. That's a pattern I can use anywhere I need a yes-no answer.
A one-line predicate is often the cleanest function you can write. No branches, no ceremony, just the question and its direct, boolean answer every time.
TL;DR: a comparison already returns a boolean — return it directly.
> — strictly greater than>= — greater than or equal< / <= — the mirror operators== — equal (two equals, not one)| Expression | spent=100, limit=100 |
|---|---|
spent > limit | False |
spent >= limit | True |
Pick the operator that matches your domain. "Over budget" usually means strictly over, not at-the-edge.