The Safe Guide
The Safe Guide supports the learner during early practice without taking over the task. For useful feedback, the learner should show a real attempt, briefly explain what is confusing them, and ask a specific question about the concept they are practicing.
The example below intentionally contains a realistic beginner mistake from Topic #23.
def sensor_alert(temp=20.5):
print("Sensor reading in Celsius:", temp)
if temp > 35.0:
return "Overheating!"
else if temp < 0.0:
return "Freezing!"
else:
return "Normal Range"
print("Final Alert:", sensor_alert(40.2))
What happens next? When you paste these questions and your code 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.
Hey there! Take a deep breath, and don’t worry, we have all been there. Staring at an else if block wondering why the computer is suddenly rebelling is basically a rite of passage for every developer. Your logic is actually spot on, and it’s clear you understand how the decision chain should flow. We just have a tiny language-specific quirk to iron out!
Since you asked a great question about the mechanics of the decision chain, let’s look at the absolute bare bones of it before we fix your script.
Python evaluates if first, tries elif next, and uses else as the final catch-all.
x = 5 # Input
if x > 10: # Core -> First check
y = "Big"
elif x > 3: # Run -> Second check (only if the first was False)
y = "Mid"
else: # Final fallback (only if ALL above were False)
y = "Small"
print(y) # Salida: Mid
🎯 Desafío Espejo: Mentally trace this snippet if x = 2. What does y become?
Phase 1: Socratic Hint
It’s fantastic that you articulated your frustration—that’s how we build strong mental models. You are absolutely right to think of if, elif, and else as one single decision chain.
Think of it like a strict bouncer at an exclusive club.
-
The
ifis the VIP list. He checks that first. If you are on it, you go in, and he stops checking. -
The
elif(Python’s version of “else if”) is the guest list. He only checks this if you weren’t a VIP. -
The
elseis the general admission line. If you didn’t match the other two, this is where you automatically end up.
If you use multiple separate if statements instead, you are cloning the bouncer. Now you have three bouncers, and they will all check your ID independently, which might lead to multiple doors opening when you only wanted one!
🔍 Code Observations:
-
Line 5: You used
else if. Many languages (like JavaScript or C++) use this, but Python likes to keep things short and uses a specific contracted keyword for it. -
Line 8/9: Look closely at the very end of your script:
return "Normal Range"print(...). Python needs these to be on separate lines so it knows when one command ends and the next begins.
🧠 Socratic Question:
If Python didn’t have a contracted keyword for else if, and you literally had to use the word else followed by a completely new if block inside of it, how would that change the indentation of your code if you had 5 different conditions to check?
🚀 Next Step:
Check the official Python documentation or your lesson notes for the exact keyword Python uses to say “else if” without adding new indentation blocks, and hit ‘enter’ right before your final print statement!
Phase 2: Full Solution
Your logic was fundamentally sound. Here is your exact code with just the syntax corrected to make Python happy!
def sensor_alert(temp=20.5):
print("Sensor reading in Celsius:", temp)
if temp > 35.0:
return "Overheating!"
elif temp < 0.0: # FIXED: Replaced 'else if' with Python's built-in 'elif' keyword
return "Freezing!"
else:
return "Normal Range"
print("Final Alert:", sensor_alert(40.2)) # FIXED: Moved print to a new line
Symbol Breakdown:
-
if: The mandatory start of any conditional chain. It evaluates a boolean expression. -
elif: Short for “else if”. It ties directly to theifabove it. Python will only check this if the precedingif(and any precedingelifs) evaluated toFalse. -
else: The optional fallback. It catches everything that slipped through theifandelifchecks. It never takes a condition of its own.
⚠️ Common Mistakes:
-
Using
else if: Developers coming from other languages frequently typeelse if. In Python, this throws a syntax error. Always useelif. -
Missing the Colons: Forgetting the
:at the end of theif,elif, orelseline. -
Using
ifinstead ofelif: Stacking multipleifstatements instead of usingelifmeans Python checks every single one, even if an earlier one was already True. This is a massive source of logical bugs!
🏋️ Mini-Challenges:
-
Extend the Chain: Add another
elifcondition to check if the temperature is exactly20.5and return"Optimal!". Where does it need to go in the chain? -
Reverse the Logic: Try rewriting the function so that the very first
ifchecks for the “Normal Range” (between 0.0 and 35.0), and theelsehandles the extreme temperatures.
—
⚡ Coding5s Mentor System — Eliminating Epistemic Debt
