PythonBit Logo

7. Conditional Statements for Decision-Making

Learn how to make your code think and decide like a game master!


Level Up with Decision-Making Powers! 🎮

Hey coding champions! Remember those awesome string powers we learned? Now it's time to learn something even cooler - how to make your code THINK and DECIDE! Just like in games where you choose different paths on your quest, conditional statements let your code make smart choices! 🧙‍♂️

Python if else conditions

Today's Quest! 🎯

You'll learn how to:

  • Make your code choose different paths like in an adventure game! 🗺️
  • Use magical comparison operators to test conditions ✨
  • Chain multiple decisions together for complex quests 🔗
  • Check if items exist in your inventory using the 'in' operator 🎒

Your First Decision Spell: if Statements! 🪄

Think of if statements like magical gates - if something is true, you get to pass through! Let's try it:

Exercise 1 • python101-7-conditions
No output yet...

🎮 Quick Game: Try changing the player_name to different values:

  • "Pi" (too short!)
  • "SuperWarrior" (perfect!)
  • "Zx" (too short!)

Level 2: Dragon Alert System! 🐲

Now let's check for special powers:

Exercise 2 • python101-7-conditions
No output yet...

Cool, right? Your code just made its first decision!

Comparison Magic: Testing Conditions! ⚔️

🔍 Time to learn the magical comparison symbols::

  • == : Is equal to
  • != : Not equal to
  • > : Greater than
  • < : Less than
  • >= : Greater than or equal to
  • <= : Less than or equal to
Exercise 3 • python101-7-conditions
No output yet...

🎮 Mini-Game Break: Guess the Output!

What do you think this code will print?

Exercise 4 • python101-7-conditions
No output yet...

Take a guess before trying it out!

Secret Inventory Checker: the 'in' Operator! 🎒

Ever wanted to check if you have an item in your backpack? Here's how:

Exercise 5 • python101-7-conditions
No output yet...

🎮 Fun Fact: The in operator is like a magical detector that can find things in lists!

Multiple Choice Paths: elif Adventures! 🗺️

Sometimes you need more than just yes/no choices. Enter elif - your new best friend!

Exercise 6 • python101-7-conditions
No output yet...

🎮 Try It: Change player_class to a different value to get You charge into battle with your sword! ⚔️!

Boss Battle: Complex Conditions! 🐉

Exercise 7 • python101-7-conditions
No output yet...

🎮 Mini-Game: The Quest Checker!

Your mission:

  1. Check if the quest is "ACTIVE"

  2. Check if the player has all required items

  3. Check if player meets minimum level (level 2)

Exercise 8 • python101-7-conditions
No output yet...

🌟 Super Challenge: Create a Game Choice System!

Create conditions to check:

  1. If player chose "cave":
  • Must have torch and hp > 50
  1. If player chose "mountain":
  • Must have hp > 70
  1. If player chose "forest":
  • Can enter anytime!

Check if they can enter cave, mountain, or the forest.

Exercise 9 • python101-7-conditions
No output yet...

🎯 Debug Mission: Fix the Game Logic!

Can you spot what's wrong with these conditions? Fix them here :)

Exercise 10 • python101-7-conditions
No output yet...

Hint: Look carefully at the type of player_power!

Exercise 11 • python101-7-conditions
No output yet...

Hint: Check the comparison operator!

Pro Tips for Victory! 🏆

Remember:

  • Always check your comparison operators (==, !=, >, <, >=, <=)
  • Pay attention to your indentation - Python is picky!
  • String comparison is case-sensitive ("Sword" != "sword")
  • Use 'and' to check multiple conditions that all must be True
  • Use 'or' when any one condition can be True

Final Boss Challenge: Create a Complete Game Check System!

Ready for the ultimate test? Create a complete game check system!

  1. Parse the player_stats (use your string powers!)

  2. Check if player is ready for dragon battle:

  • Level must be 5 or higher
  • Health must be above 80
  • Must have both Sword and Shield
  • Current quest must be "DefeatDragon"
  1. Print appropriate messages for each condition
Exercise 12 • python101-7-conditions
No output yet...

🎉 Congratulations!

You've learned the power of making decisions in code! Now you can:

  • Make your programs think and choose paths 🤔
  • Check conditions like a pro 🔍
  • Create complex decision trees 🌳
  • Build awesome game logic! 🎮

Ready for more coding adventures? Let's go! 🚀

Great job finishing the tutorial!

Ready to test your knowledge?

Take a quick quiz to reinforce what you've learned and make sure you've mastered the key concepts.