LEARN Python - 2.2 Else When Python Needs a Backup Plan - zerotopyhero.com

Learn Python – 2.2: Else – When Python Needs a Backup Plan

In the last lesson, you taught Python how to ask a question.

“If this is true, do something.”

That was a big step.
But there’s still a tiny problem.

When the answer is false, Python just… says nothing.

No message.
No reaction.
No explanation.

Sometimes that’s fine.
But most of the time, silence is confusing.

That’s where else comes in.

The Problem with Silence

Let’s look at this again:

				
					age = int(input("How old are you? "))

if age >= 18:
    print("You are allowed in.")
				
			

If the user types 20, they get a message.
If they type 15, the program ends quietly.

Beginners often think:

“Did my code break?”

Nope.
Python just didn’t have instructions for what to do otherwise.

So let’s give it some.

Enter: else

else literally means:

“If the condition above is false, do this instead.”

Here’s the same example, upgraded:

				
					age = int(input("How old are you? "))

if age >= 18:
    print("You are allowed in.")
else:
    print("Sorry, you're not old enough.")
				
			

Now Python always responds.

One path runs if the condition is true.
The other path runs if it’s false.

Exactly one of them will run.
Never both.

How Python Chooses

Python reads this as:

  1. Check the condition

  2. If it’s true → run the if block

  3. Otherwise → run the else block

  4. Then continue with the program

There’s no guessing.
No randomness.
Just a clean decision.

Think of it like a fork in the road:

  • Left path: condition is true

  • Right path: condition is false

Python picks one and keeps walking.

Indentation Still Matters (A Lot)

Just like with if, indentation tells Python what belongs where.

				
					if condition:
    this_runs_if_true
else:
    this_runs_if_false
				
			

Both blocks must line up properly.

If the indentation is wrong, Python won’t “try its best.”
It will stop and complain.

That’s a good thing.
It keeps your logic clear and predictable.

A Friendly Example

				
					name = input("What's your name? ")

if name == "Alex":
    print("Hey Alex! Welcome back.")
else:
    print("Nice to meet you!")

				
			

Only Alex gets the special greeting.
Everyone else still gets something friendly.

No awkward silence.

You Can Only Have One else

An if statement can have:

  • One if

  • Optional else

That’s it.

else always belongs to the closest if above it.

Python reads code top to bottom, so placement matters.

Thinking in Plain English (Again)

Before writing code, say it out loud:

  • If the password is correct, allow access

  • Otherwise, deny access

  • If the number is even, say “Even”

  • Otherwise, say “Odd”

If you can say it clearly, the code almost writes itself.

What You’ve Learned

You now know how to:

  • Handle both outcomes of a condition

  • Prevent silent programs

  • Give Python a clear backup plan

  • Build simple two-path logic

This is the foundation of real decision-making.

Mini Quiz

Try these:

  1. What does else mean in plain English?

  2. Will both if and else ever run?

  3. What happens if the condition is false and there’s no else?

  4. Why is indentation important in if / else blocks?

  5. What will this code print?

				
					number = 3

if number > 5:
    print("Big")
else:
    print("Small")

				
			
ZeroToPyHero