Week 5 - Conditionals: If/Else for Decision-Making

Ridwan IbidunniRidwan Ibidunni
3 min read

Hello Future Coders!

Welcome to Week 5 of our Python Adventures! This week, we're going to learn how to help our programs make decisions using something called conditionals. Think of conditionals like the brain of your program—they decide what to do in different situations, just like you decide whether to take an umbrella when it rains or go out when it's sunny!


What Are Conditionals?

Conditionals let our program choose between different actions based on a simple yes-or-no question. For example, when you wake up in the morning:

  • If it’s raining, you take an umbrella.

  • Else (if it’s not raining), you leave it at home.

In Python, we write this as an if/else statement to help our computer make choices.


How Do We Write an If/Else Statement?

Here's a simple way to think about it:

if condition:
    # do something
else:
    # do something else

Let’s Look at a Fun Example:

Imagine we want our program to decide what to do based on the weather. We can ask the user:

weather = input("Is it raining? (yes/no) ")

if weather == "yes":
    print("Take your umbrella!")
else:
    print("Enjoy the sunshine!")
  • What happens here?
    The program asks, “Is it raining?”

    • If you answer "yes", it tells you to take your umbrella!

    • If you answer anything else (like "no"), it tells you to enjoy the sunshine!


Let’s Practice!

Challenge 1: Weather Checker

Try writing a small program that asks, “Is it cold outside?” and then prints:

  • “Wear a jacket!” if you answer yes.

  • “No jacket needed!” if you answer no.

Challenge 2: Energy Levels

Imagine your robot has energy:

  • Ask: “How much energy does your robot have? (Enter a number)”

  • If the energy is high (more than 70), print: “Your robot is super energetic and ready for action!”

  • If the energy is medium (more than 40 but 70 or less), print: “Your robot can play a bit.”

  • Otherwise, print: “Your robot is tired and needs a nap!”

Here’s a hint for the code:

energy = int(input("Enter your robot's energy level (0-100): "))

if energy > 70:
    print("Your robot is super energetic and ready for action!")
elif energy > 40:
    print("Your robot has some energy and can play a bit.")
else:
    print("Your robot is tired. It needs a nap!")

Quiz Time!

  1. What does an if/else statement do?

    • A) Repeats a set of commands

    • B) Helps the program decide between two actions

    • C) Prints messages on the screen

  2. In the weather example, if you answer "no" to "Is it raining?" what will the program do?

    • A) Tell you to take an umbrella

    • B) Tell you to enjoy the sunshine

    • C) Show an error

  3. How do we convert the user's input into a number for our energy challenge?

    • A) Using input()

    • B) Using int(input())

    • C) Using print()


Recap & Reflection

Key Points:

  • Conditionals are like decision points that help our programs choose what to do.

  • If/else statements work like this: if a condition is true, do one thing; if not, do another.

  • Using conditionals makes your programs smarter and more interactive!

Think About It:
Imagine you’re designing a super-smart robot. What kind of decisions should it make? If it's hungry, should it look for food, or if it’s sleepy, should it take a nap? Write down your ideas and be ready to share them next time!


What’s Next?

Next week, we’ll explore loops—the cool tools that help our programs repeat actions over and over. Get ready to make your robots dance and perform fun routines!


Happy Coding, Future Coders!
Keep practicing, have fun, and see you next week for more Python Adventures!


0
Subscribe to my newsletter

Read articles from Ridwan Ibidunni directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ridwan Ibidunni
Ridwan Ibidunni

I'm a mathematician that loves its applications in all spheres of life, especially in the field of machine learning. I write Java, Python and Android applications