9 The Grail Castle Test: Mastering ‘If’ Statements
In our previous lesson, we learned about logical operators. Today, we’ll use that knowledge to enter the Grail Castle of conditional statements, focusing on the powerful ‘if
’ statement. This magical construct will allow our code to make decisions, just as a knight must choose their path wisely.
9.1 What are Conditional Statements?
Imagine you’re standing at a crossroads in your quest. The path you choose depends on certain conditions - is it raining? Do you have enough provisions? Are there dragons ahead? In Python, we use conditional statements to make these kinds of decisions in our code.
The ‘if
’ statement is the most basic form of conditional statement. It allows us to execute a block of code only if a certain condition is true.
9.2 The Structure of an ‘If’ Statement
Here’s the basic structure of an ‘if
’ statement:
if condition:
# Code to execute if the condition is True
# This block is indented
Let’s break this down:
- The statement starts with the keyword
if
. - After
if
, we have a condition - an expression that evaluates to eitherTrue
orFalse
. - The condition is followed by a colon
:
. - The next line starts an indented block of code. This block only runs if the condition is
True
.
9.3 Your First ‘If’ Statement
Let’s cast our first ‘if
’ statement spell:
= True
is_knight
if is_knight:
print("You may enter the Grail Castle.")
print("This line always runs.")
This will output:
You may enter the Grail Castle.
This line always runs.
If we change is_knight
to False
, we’ll only see:
This line always runs.
9.4 Using Comparison Operators in Conditions
We often use comparison operators in our conditions. Here’s a list of these operators:
==
: Equal to!=
: Not equal to>
: Greater than<
: Less than>=
: Greater than or equal to<=
: Less than or equal to
Let’s use these in some ‘if’ statements:
= 7
knight_level
if knight_level >= 5:
print("You are experienced enough for this quest.")
= 20
magic_power
if magic_power > 50:
print("You can cast a powerful spell!")
= "Camelot"
password
if password == "Camelot":
print("Access granted to the Round Table.")
9.5 Combining Conditions with Logical Operators
Remember our logical operators (and
, or
, not
) from the previous lesson? We can use these to create more complex conditions:
= True
has_sword = False
has_shield = True
is_brave
if has_sword and is_brave:
print("You are ready to face the dragon!")
if has_sword or has_shield:
print("You have at least one piece of equipment.")
if not has_shield:
print("You might want to buy a shield.")
9.6 Practice Your ‘If’ Statement Magic
Now it’s your turn to practice these conditional spells:
Create a variable
dragon_hp
(hit points) and set it to a number. Write an ‘if
’ statement that prints “The dragon is defeated!” ifdragon_hp
is 0 or less.Make variables for
has_sword
,has_armor
, andhas_magic
. Write an ‘if
’ statement that prints “You are fully equipped!” if all three areTrue
.Create a
player_gold
variable. Write an ‘if
’ statement that prints “You can buy a potion” ifplayer_gold
is at least 50.
Here’s a starting point for your practice:
# Quest 1: Defeat the Dragon
= 100 # Change this value to test your code
dragon_hp # Your code here
# Quest 2: Check Equipment
= True
has_sword = False
has_armor = True
has_magic # Your code here
# Quest 3: Buy a Potion
= 75
player_gold # Your code here
# Quest 4: Weather Check
= True
is_day = False
is_sunny # Your code here
9.7 Common Bugs to Watch Out For
As you cast your ‘if
’ statement spells, beware of these common pitfalls:
Forgetting the colon: Always remember to put a colon
:
at the end of your ‘if’ line.Incorrect indentation: The code block under an ‘if’ statement must be indented. Incorrect indentation can change the meaning of your code.
Using
=
instead of==
: Remember,=
is for assignment,==
is for comparison.Misusing
and
andor
: Be clear about your logic.and
requires all conditions to beTrue
, whileor
only requires one.Unnecessary
if
statements: You don’t need an ‘if’ statement to check a boolean variable. Instead ofif is_knight == True:
, you can simply writeif is_knight:
.
9.8 Conclusion and Further Resources
You’ve now mastered the art of ‘if
’ statements, a crucial skill in your coding arsenal. With this power, your programs can now make decisions and respond to different conditions, just like a real knight on a quest.
To further hone your ‘if
’ statement skills, check out these valuable resources:
- Python Official Documentation on ‘if’ statements
- Real Python’s Python Conditional Statements
- W3Schools Python Conditions
Remember, every great Python sorcerer started where you are now. Keep practicing your ‘if
’ statements, and soon you’ll be weaving complex decision-making logic into your code with ease. Onward to your next coding challenge!