Lesson 3 · Deductive Reasoning

If This, Then That: Making Decisions Like a Logic Ninja

What You'll Learn

How to build rules that make decisions automatically — and how to combine multiple conditions to handle complex, real-world situations.

Your Brain Is a Decision Machine

You make hundreds of decisions every day without even thinking about them. Should I wear a jacket? Is it worth running to catch the bus? Should I text back now or later? Can I stay up another 30 minutes?

Here's the interesting part: most of these decisions follow the same pattern. If something is true, then you do one thing. Otherwise, you do something else.

If it's cold outside → wear a jacket.
If it's warm → don't bother.

This pattern has a name in logic and philosophy: deductive reasoning. It means starting with a rule and applying it to a specific situation to reach a conclusion. If you know the rule and you know the facts, the answer follows automatically.

We've already used if and else a little bit in Lessons 1 and 2. Today, we're going to master them — and build a decision-making program that's actually useful.

Quick Review

You've seen if statements before. Let's start with a simple one to warm up:

Python · Review

Try changing level to different numbers and see which message appears. Today we're going to understand exactly how this works — and build much more powerful versions.

Asking True-or-False Questions

Every if statement works by asking a question that has only two possible answers: True or False. Python checks the answer and decides what to do.

Let's see what these questions look like:

Python

Comparison Operators

< means "less than" — 5 < 10 is True

> means "greater than" — 5 > 10 is False

== means "equal to" — remember, it's TWO equals signs (one = is for storing values)

>= means "greater than or equal to"

<= means "less than or equal to"

!= means "not equal to"

Change the temperature to 10, then 25, then 0. Watch how the True/False answers change. These comparisons are the foundation of every decision your program will ever make.

Making Decisions With if

Now let's use these True/False questions to actually do things. The basic pattern is simple:

Python

How Python Reads This

Python checks conditions from top to bottom, and stops at the first one that's True.

If temperature is 5: Python checks 5 < 10 → True! It runs the first block and skips everything else.

If temperature is 15: Python checks 15 < 10 → False. Moves to elif. Checks 15 < 20 → True! Runs that block.

If temperature is 25: Both if and elif are False, so Python runs the else block — the "everything else" catch-all.

This is deductive reasoning in action: you have rules ("if below 10, it's cold"), you have a fact (the temperature), and the conclusion follows automatically.

A Common Mistake

What if you used separate if statements instead of elif? Let's see:

Python · Spot the Problem

Why This Is Wrong

With separate if statements, Python checks every single one independently. A temperature of 5 matches all three conditions, so all three messages print.

With if/elif/else, Python stops at the first match. That's what we want — one clear answer, not three conflicting ones.

This is an important lesson in logical thinking: the order and structure of your rules matter as much as the rules themselves.

Comparing Text, Not Just Numbers

So far we've compared numbers (temperature < 10). But you can also compare text:

Python

Important Detail

When comparing text, it has to match exactly. "rainy" is not the same as "Rainy" or "RAINY".

A good trick: use .lower() to convert the user's input to lowercase before comparing. That way, "Rainy", "RAINY", and "rainy" all work. Try adding weather = weather.lower() after the input line.

Combining Conditions With and / or

Real decisions are rarely based on just one thing. Should you wear a jacket? That depends on the temperature and whether it's raining and what you're doing outside.

Python lets you combine conditions using two special words: and and or.

Python · Using and

How and Works

and means both conditions must be True for the whole thing to be True.

temperature < 15 and raining == "yes" is only True when it's BOTH cold AND rainy.

If either one is False (warm but rainy, or cold but dry), the and condition is False, and Python moves to the next elif.

Now let's see or:

Python · Using or

How or Works

or means at least one condition must be True.

activity == "sports" or activity == "walking" is True if the activity is sports, OR if it's walking, OR if it's both (though it can only be one at a time here).

Think of and as strict (both must be true) and or as flexible (either one works).

Building the Jacket Decision Helper

Let's decompose our decision helper (remember decomposition from Lesson 1?) into clear steps:

The Plan

Step 1: Ask the user for the facts: temperature, weather, activity

Step 2: Decide what to wear based on the rules

Step 3: Display the recommendation clearly

But before we write the code, let's think about the rules. This is the deductive reasoning part — we need to define clear rules that cover all the situations:

Our Rules

If temperature ≤ 0 → Heavy winter coat, no matter what

If temperature 1-10 and raining → Waterproof jacket

If temperature 1-10 and dry → Warm jacket

If temperature 11-18 and raining → Light waterproof jacket

If temperature 11-18 and active → Maybe a hoodie

If temperature 11-18 and sitting → Light jacket

If temperature > 18 → No jacket needed (maybe an umbrella if raining)

See how we're covering all the combinations? That's what good logical thinking looks like — making sure there are no gaps in your reasoning.

Python · The Jacket Decision Helper

Try different combinations: 5° and rainy with sports. 25° and sunny with sitting. -3° and snowy. See how the rules handle each case differently.

New Thing · if Inside if

You might have noticed that some if statements are inside other if statements. This is called nesting.

For example: first we check if it's freezing (temp <= 0). Once we know it IS freezing, we check whether it's also wet. This is like narrowing down your decision: "OK, it's cold — but is it also raining?"

Python knows which if belongs inside which by looking at the indentation (the spaces). More spaces = deeper level. This is why indentation matters in Python — it tells Python the structure of your logic.

Storing Decisions in Variables

Did you notice these two lines in the Jacket Helper?

Python

Why This Is Smart

Instead of writing if weather == "rainy" or weather == "snowy": over and over, we calculate it once and store it: is_wet = weather == "rainy" or weather == "snowy".

Now is_wet is either True or False — a simple answer we can reuse everywhere.

This is actually abstraction again — we're taking a complex check and giving it a simple name. Two thinking skills working together!

Level Up: The Weekend Activity Planner

Let's use everything we've learned to build something even cooler — a program that recommends what you should do this weekend based on multiple factors:

Python · Weekend Planner

Try every combination you can think of. Notice how the order of the conditions matters — Python checks from top to bottom and stops at the first match. The most specific conditions (sunny + energetic + friends) come first, and the most general ones come last.

What You Just Did

You built programs that think logically — that take in facts, apply rules, and produce conclusions. That's exactly what deductive reasoning is.

The key insight: if your rules are clear and complete, the right answer follows automatically. You don't need to figure it out each time — the rules do the work for you.

This is how GPS navigation works (if distance is shorter, take this road), how thermostats work (if temperature is below the setting, turn on heat), and how video games work (if player health reaches 0, game over).

The Big Idea

When you need to make a decision, ask: "What are my rules? What are the facts? What conclusion follows?" Clear rules + clear facts = clear answers. And if you can write the rules in code, the computer can make the decision for you — perfectly, every time.

Thinking Questions

Talk these over with someone, or think about them quietly:

  1. What happens if your rules have gaps — situations they don't cover? Can you think of an input that would break the Weekend Planner?
  2. Why does the order of if/elif/else matter? What would happen if we checked "sunny" before "sunny + friends + energy"?
  3. Can you think of a real-life decision you make that could be turned into if/elif/else rules?

Level Up

Each challenge requires you to design clear rules. Think about all the possible cases before writing any code.

Challenge 1 · Grade Calculator

Ask the user for a test score (0-100) and display the letter grade: A (90+), B (80-89), C (70-79), D (60-69), F (below 60). Add an encouraging message for each grade.

Challenge 2 · What Should I Eat?

Build a meal recommender that asks: How hungry are you? (starving/normal/snack) Do you want something healthy? (yes/no) Do you want something quick? (yes/no) Then recommend a specific food based on the combination.

Challenge 3 · Movie Night Picker

Ask the user: What mood are you in? (happy/scared/thoughtful) Who are you watching with? (alone/friends/family) Do you want something short? (yes/no) Then recommend a movie genre and explain why it fits. Use and and or to handle the combinations.

What You Learned

Thinking Skill

Deductive Reasoning — if the rules are true and the facts are true, the conclusion must be true. Design clear rules, gather the facts, let logic do the rest.

Python Concepts

Comparison operators< > == != >= <=

True and False — every comparison produces one of these

if / elif / else in depth — checking from top to bottom, stopping at first match

and — both conditions must be True

or — at least one condition must be True

Nested if statements — decisions inside decisions

.lower() — convert text to lowercase for easier comparison

Boolean variables — storing True/False in a variable: is_wet = weather == "rainy"

Lessons 1 + 2 + 3

Decomposition — break big problems into small steps

Abstraction — decide what details matter and ignore the rest

Deductive Reasoning — design rules, gather facts, reach conclusions