Part 3: Control Flow — If, Else, and Making Decisions (with AI-Powered Practice)

So far, you’ve learned how to write Python scripts that take input and display output. Now it’s time to make your programs smart — capable of making decisions.

In this post, we’ll cover:

  • Conditional logic using ifelif, and else
  • Comparison and logical operators
  • Real examples and AI tools to help you practice and test logic

🔁 What Is Control Flow?

Control flow allows your program to make decisions and run different code depending on certain conditions. Python uses:

  • if — checks a condition
  • elif — checks another condition if the first is false
  • else — runs if none of the above are true

🧪 Example 1: Simple Conditional

age = int(input("Enter your age: "))

if age >= 18:
print("You're an adult!")
else:
print("You're a minor.")

Try changing the age to test different outcomes!


🧠 AI Tip: Let ChatGPT Quiz You on Conditionals

Ask ChatGPT:

“Can you give me 5 beginner challenges using if/else logic in Python?”

It might respond with challenges like:

  • Check if a number is even or odd
  • Grade a test score
  • Recommend weather attire based on temperature

🔍 Comparison Operators

OperatorMeaningExample
==Equalsx == 5
!=Not equalx != 3
>Greater thanx > 10
<Less thanx < 7
>=Greater or equalx >= 18
<=Less or equalx <= 100

🔗 Logical Operators

Use these to combine multiple conditions:

  • and — all conditions must be true
  • or — at least one must be true
  • not — inverts the condition
temperature = 75
is_raining = False

if temperature > 70 and not is_raining:
print("It's a great day for a walk!")

🤖 Use AI to Analyze Logic

Try this in ChatGPT:

“Explain this code and how the condition works: if score >= 90 and attendance >= 80:

You’ll get a step-by-step breakdown of how the logic is evaluated.


🎯 Practice Challenge

Write a program that:

  1. Asks for a user’s exam score (0–100)
  2. Prints:
    • "A" if score ≥ 90
    • "B" if score ≥ 80
    • "C" if score ≥ 70
    • "D" if score ≥ 60
    • "F" otherwise

Bonus: Ask ChatGPT to help you add input validation or suggest a different grading scale!


🛠️ Tools to Reinforce Learning


🏁 Coming Up in Part 4…

In the next post, we’ll dive into loops — the key to repeating actions and building simple games. You’ll learn about forand while loops, and we’ll use AI to generate custom loop challenges.

Bonus preview: We’ll create a simple “number guessing game”!

Share the Post:

Related Posts

Join the Codexa Crew – It’s free!

Stay Updated with the Latest in Tech & Automation

Be the first to know when new content drops on Green Codexa!