Part 3: The Crossroads of the Cosmos — Making Decisions
The Adventures of Ava the Astronaut: Learning Python One Planet at a Time
Part 3: The Crossroads of the Cosmos — Making Decisions
The Adventures of Ava the Astronaut: Learning Python One Planet at a Time
A Fork in the Path
After mastering the art of storing memories on Planet Xylos, Ava and her AI companion, Py, warped to their next destination. Suddenly, the ship’s alarm blared. On the main screen, their path split into two — a fiery, red nebula to the left and a calm, blue asteroid field to the right.
Py’s robotic voice chirped:
“Commander, we have reached the Crossroads of the Cosmos. Our sensors detect that the red nebula is dangerously hot, while the blue field is safe. We must choose a path.”
Ava realized that her programs so far have been like a straight road: they execute one command after another, from top to bottom. But now, she needed her program to look at the situation and decide what to do. This is where conditional logic comes in — the ability for a program to make choices.
Questions and Answers: The if Statement
In life, we make decisions based on conditions all the time. If it is raining, you take an umbrella. If you are hungry, you eat. If the traffic light is green, you go.
Programming works the same way. We use conditional statements to ask questions and perform different actions based on the answers. The simplest way to do this in Python is with an if statement. An if statement checks if a certain condition is True. If it is, the block of code underneath it runs. If it is False, the code is simply skipped.
Let’s help Ava decide whether to enter the nebula. First, we need to check the temperature.
# The temperature of the red nebulanebula_temperature = 5000
# We ask a question with an if statementif nebula_temperature > 4000: print("Warning: Nebula is too hot! Avoid this path.")
print("Decision-making process complete.")What happens?
The output will be:
Warning: Nebula is too hot! Avoid this path.Decision-making process complete.Because the condition nebula_temperature > 4000 was True (5000 is indeed greater than 4000), the warning message was printed. Two very important things to notice here: the colon (:) at the end of the if line, and the indentation (the four spaces before print). The indentation is how Python knows which code belongs inside the if block. Everything indented under the if is part of its body.
The Cosmic Comparison Operators
To ask questions, we need to compare things. Python gives us a set of comparison operators to do this. Each one produces a boolean result: either True or False.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 6 | True |
> | Greater than | 10 > 5 | True |
< | Less than | 3 < 8 | True |
>= | Greater than or equal to | 7 >= 7 | True |
<= | Less than or equal to | 4 <= 2 | False |
Watch out! A very common mistake for beginners is using a single equals sign (
=) for comparison. Remember:=is for assigning a value to a variable (putting something in a locker), while==is for comparing if two values are equal (asking a question). They look similar but do very different things!
The Other Path: if-else
What if a condition is false? We might want to do something else instead of just skipping the code. The else statement provides an alternative block of code that runs only when the if condition is False. It is like saying, “If it’s sunny, go to the park; otherwise, stay home and read a book.”
ship_fuel = 30 # Fuel percentage
if ship_fuel >= 50: print("Fuel levels are good. Ready for a long journey!")else: print("Warning: Low fuel. We must find a refueling station.")Here, since ship_fuel (30) is less than 50, the if condition is False. So, the program skips the first block and jumps straight to the else block, printing the warning message.
Many Paths: if-elif-else
Sometimes there are more than two choices. Imagine a planet with three landing zones: one for Commanders, one for Captains, and one for everyone else. For this, we use elif, which is short for “else if.” It lets you check multiple conditions in sequence.
user_rank = "Captain"
if user_rank == "Commander": print("Access granted to Landing Zone A (VIP).")elif user_rank == "Captain": print("Access granted to Landing Zone B.")elif user_rank == "Lieutenant": print("Access granted to Landing Zone C.")else: print("Please proceed to the Public Landing Zone D.")Python checks the conditions from top to bottom. As soon as it finds one that is True, it runs that code block and skips all the rest. If none of the if or elif conditions are true, the else block at the bottom acts as a catch-all.
Combining Conditions: Logical Operators
What if Ava needs to check more than one thing at the same time? For example, she can only enter a planet if she has enough fuel and the right clearance. Python provides logical operators to combine conditions.
| Operator | Meaning | Example | Result |
|---|---|---|---|
and | Both conditions must be True | True and False | False |
or | At least one condition must be True | True or False | True |
not | Reverses the condition | not True | False |
Here is an example of Ava checking her entry clearance:
fuel_level = 60clearance = "Green"
if fuel_level >= 50 and clearance == "Green": print("All systems go! You are cleared for landing.")elif fuel_level < 50: print("Denied: Insufficient fuel for landing.")else: print("Denied: Your security clearance is not valid.")The first condition checks two things at once: is the fuel at least 50 and is the clearance “Green”? Both must be true for the landing to be approved.
Mini-Project: The “Planet Entry Checker”
Your mission is to build a security system for a newly discovered planet called Veridia. The system needs to check a few things before allowing a visitor to enter.
The Rules of Veridia:
- The visitor must be at least 10 years old.
- They must have a fuel level of at least 20%.
- They must have a security clearance of either “Green” or “Blue”.
Your Task:
- Create variables for
age,fuel_level, andsecurity_clearance(you can set them directly or useinput()). - Write a program using
if,elif, andelsestatements to check these conditions. - Use the
andandorlogical operators to combine checks where needed. - Print different messages based on the outcome: a welcome message if all conditions are met, or a specific rejection message telling the user which rule they violated.
Example:
age = 12fuel_level = 55security_clearance = "Green"
if age >= 10 and fuel_level >= 20 and (security_clearance == "Green" or security_clearance == "Blue"): print("Welcome to Planet Veridia!")elif age < 10: print("Entry denied: You must be at least 10 years old.")elif fuel_level < 20: print("Entry denied: Insufficient fuel. Minimum 20% required.")else: print("Entry denied: Invalid security clearance. Must be Green or Blue.")Try changing the values of the variables to see different outcomes!
Mission Complete!
You have successfully navigated the Crossroads of the Cosmos! You can now write programs that make intelligent decisions using if, elif, and else. You have learned to compare values with comparison operators and to combine conditions with logical operators like and, or, and not. Your programs are no longer just straight lines — they can now branch and adapt.
Get ready for our next mission! We are heading to the “Looping Nebula,” where we will learn how to make our programs repeat actions automatically, saving us from doing the same work over and over again! See you in Part 4!
This is Part 3 of a 10-part series: “The Adventures of Ava the Astronaut: Learning Python One Planet at a Time.”