...

/

Making Decisions with AI

Making Decisions with AI

Learn Python if / else by building a smart thermostat that decides whether to turn on the heater or air conditioning.

The project

You’ve just moved into a great neighborhood, but your new home needs an upgrade to its automated temperature control system. At the moment, the thermostat is fairly basic—it can’t determine whether to turn on the air-conditioning or the heater. A static display that says Temperature is 25, isn’t very useful. What you really need is a system that can make decisions based on the temperature and your individual comfort level.

Press + to interact

With your confidence in your Python skills now, you aim to create this thermostat logic yourself! This smart home system should compare the current room temperature with the users’ preferred comfort temperature and then decide what to do.

The programming concept

Imagine this:

You’re writing a program, and you want it to say:

  • You can vote! If the person is 18 or older.

  • Sorry, too young. If they’re younger than 18.

How would you say that in plain English?

If age is 18 or more → say yes. Otherwise → say no.

Python can do that. Let’s try it together:

age = input("How old are you? ")
age = int(age)
if age >= 18:
print("You can vote!")
else:
print("Sorry, too young.")
If-Else in Python

How do you read this code? You’ve already seen what input() and int() do. In lines 1 and 2, it asks the user for their age and then converts that from text input to an integer or a number. This conversion was important because we want to compare the input age with 18. What we set out to do in simple English earlier is being translated into Python in lines 4–7. It’s easy to read, right?

The significance of the concept in the project

Back to the house. Let’s say the current temperature in the house is 15°C. If your comfort temperature is 18°C, then the thermostat should decide to turn on the fan mode. If instead the current temperature is 25°C and your comfort temperature is 22°C, then the thermostat should turn on the AC mode.

The point is, the system should not just print numbers; it should decide actions based on the situation. That’s the power of if / else.

Concept at a glance:

  • if condition: Checks whether something is true.

  • else: Runs if the condition is false.

  • Don’t forget the colon, :.

  • Python uses indentation (spaces) to know what belongs inside the if.

  • Comparison operators are:

    • < for less than, <= for less than or equal to

    • > for greater than, >= for greater than or equal to

    • == for checking equality

Learn to code the concept

Let’s start with a very simple example:

Press + to interact
Python 3.5
temperature = 15
if temperature < 18:
print("Switch to Fan mode")
else:
print("Keep cooling")

Here, Python checks if temperature < 18. If true, it prints Switch to Fan mode. Otherwise, it prints Keep cooling. But this solution is hardcoded. What if the homeowner prefers a different comfort temperature? Let’s make it more flexible by introducing a variable for preferred_temperature.

Press + to interact
Python 3.5
current_temperature = 15
preferred_temperature = 18
if current_temperature < preferred_temperature:
print("Switch to Fan mode")
else:
print("Keep cooling")

Now, if you change the preferred temperature in just one place, the logic will still work correctly.

Learn to code with AI

Now that we’ve learned the concept of if / else, let’s revisit the project we had to build: a system that checks the current temperature against the preferred comfort temperature and gives advice.

Here’s the sample output of the smart homes app we want:

Enter your preferred comfort temperature: 22
Preferred comfort temperature: 22
Current temperature: 25
Action: Turn on AC
Tip: Wait until non-peak hours to save energy.
User Interface for smart homes app

Generate code with AI

You already know by now how to talk to your AI buddy. Give it some details of what you want to build, and let's see how it guides you:

Powered by AI
5 Prompts Remaining
Prompt AI WidgetOur tool is designed to help you to understand concepts and ask any follow up questions. Ask a question to get started.

Test code with AI

Once the AI has generated the code, copy-paste it into the following code widget, run it, ask the AI Mentor for feedback, and improve the code if necessary.

# Write your code for the thermostat advisor
If temeperature is below 18, turn on heater

If you’re stuck, click the “Show Solution” button.

Practice makes perfect

Now that you’ve successfully built a simple decision-making system, let’s try a different scenario. Imagine you’re creating a Smart Washing Machine Advisor that helps users decide when to run their laundry while keeping energy bills low.

Here’s what your system should do:

  • If the laundry load size is less than 5 kg → Advice: Use Quick Wash

  • If the laundry load size is exactly 5 kg → Advice: Either mode works

  • If the laundry load size is more than 5 kg → Advice: Use Full Wash

Add an extra variable called is_peak_hour (set it to True or False).

  • If it’s peak hour and the advice is not "Either mode works," → print an extra tip:
    "Tip: Wait until non-peak hours to save on electricity bills."

At the very end of the program, always print:
Stay clean and save energy!

Problem

This is what needs to be generated now:

Laundry load size: 7
Peak hour: True
Advice: Use Full Wash
Tip: Wait until non-peak hours to save on electricity bills.
Stay clean and save energy!

Code its solution

Complete this code and test it with AI mentor.

# Smart Washing Machine Advisor

# --- Variables ---
laundry_load = 7  # in kilograms
is_peak_hour = True  # Change to False for non-peak hours

# --- Decision Logic ---


# Extra energy-saving advice

print("Stay clean and save energy!")
Smart Washing Machine Advisor

If you’re stuck, click the “Show Solution” button.

Perfect! Now you’ve created a smart sashing machine advisor that adapts to the homeowner’s comfort.