The Logical Scaffold
The Logical Scaffold helps the learner connect missing pieces instead of completing them automatically. For useful feedback, the learner should provide their partial implementation, explain where the logic currently stops, describe the result they still need to reach, and ask for guidance on the missing transformation.
The example below simulates a student who successfully completed part of Topic #23 but cannot connect the remaining input case to the required output.
Age 10 is categorized as: Child
Age 15 is categorized as: Teen
Age 25 is categorized as: Unknown
Age 18 is categorized as: Teen
def categorize_user(age):
category = "Unknown"
if age < 13:
category = "Child"
elif age < 20:
category = "Teen"
return category
users = [10, 15, 25, 18]
for u in users:
print(f"Age {u} is categorized as: {categorize_user(u)}")
What happens next? When you paste this partial implementation and your question immediately after the Mentor Prompt, you receive feedback like the example shown below.
Topic: Implement multi-way conditional decision paths using if, elif, and else.
Welcome! You have laid down an excellent foundation, now let us place the final puzzle piece. It is entirely normal to reach this stage and second-guess the blueprint. The fact that you are pausing to ask “should this be an elif or an else?” means you are thinking like a true architect of logic.
Let’s look at the flow of your materials and figure out exactly what the roof should look like.
The Data Gap Analysis:
-
Point A (The Input): An integer representing a user’s age. For example,
25. -
Point B (Current State): Your code successfully routes ages
< 13to “Child” and ages< 20to “Teen”. But when the age is25, it fails both tests, falls completely through your conditional structure, and returns the default starting value:"Unknown". -
Point C (Required Output): The data needs to be captured and transformed into
"Adult"before the function returns.
Structural Hint:
Think of a conditional chain like sorting mail. if is your first specific sorting bin. elif is your second specific sorting bin. Now, you have a pile of mail remaining. If you need to sort some of that remaining mail into a specific “Adult” bin and the rest into a “Senior” bin, you need another specific label. But, if you want every single piece of remaining mail to go into the “Adult” bin, you don’t need to read the labels anymoreβyou just need a giant catch-all bucket at the end of the line.
Code Observations:
-
You have smartly initialized
category = "Unknown"on line 2. This is a great safety net so your function always has something to return. -
Your routing logic relies on the cascading nature of
if/elif. Because< 13catches everything up to 12, your< 20check safely assumes the age is already 13 or higher. This is very clean logic!
Socratic Question:
Since we want everyone who bypassed the first two checks (meaning they are mathematically guaranteed to be 20 or older) to be categorized as an “Adult”, do we really need to ask Python to evaluate another mathematical condition like age >= 20, or can we just use the structural keyword that means “for absolutely everything else”?
Next Step:
Check your Python documentation or notes on the difference between elif (which requires a specific condition to evaluate to True) and else (which requires no condition and acts as the ultimate fallback). Decide which one fits your blueprint best, and add those two lines to the end of your chain!
Phase 2: Full Solution
You built a fantastic foundation here. Your use of a cascading threshold (< 13, then < 20) proves you understand how the decision tree evaluates from top to bottom. Here is the completed structure:
def categorize_user(age):
category = "Unknown"
if age < 13:
category = "Child"
elif age < 20:
category = "Teen"
else:
# ADDED: The catch-all bucket for anything 20 or greater
category = "Adult"
return category
users = [10, 15, 25, 18]
for u in users:
print(f"Age {u} is categorized as: {categorize_user(u)}")
The Clean Connection:
Using else is the most idiomatic and efficient way to close this logic gap. While you could have written elif age >= 20:, it is redundant. By the time the Python interpreter bypasses the < 20 check, the laws of mathematics dictate the age must be 20 or higher. Using else explicitly tells other programmers reading your code: “This is the final fallback state for all remaining valid inputs.”
Positive Reinforcement:
I want to explicitly praise your setup on line 2: category = "Unknown". This is called defining a default state, and it prevents a very common UnboundLocalError if the conditional chain somehow failed to assign a value. Great architectural habit!
β οΈ Common Mistakes:
-
Over-engineering: Writing
elif age >= 20:instead ofelse:. While it works, it forces the computer to do unnecessary math and clutters the code. -
Misunderstanding
elsesyntax: Trying to giveelsea condition (e.g.,else age >= 20:).elsetakes no arguments; it just takes a colon:.
ποΈ Mini-Challenges:
-
The Senior Addition: The requirements changed! Anyone 65 or older must now be categorized as “Senior”. How will you adjust your chain using a new
elifwhile keepingelseas the fallback for Adults? -
Negative Ages: What happens right now if you pass
-5into your function? Modify the logic so that any age less than0returns"Invalid Age". Where should this check be placed in the chain?
—
β‘ Coding5s Mentor System β Eliminating Epistemic Debt & Connecting the Flow
