Today’s lesson was one of the most exciting lessons so far because we moved deeper into decision-making in Python by learning how to control the flow of our program using conditional statements.
The final project for today was called Treasure Island, a simple adventure game where users make choices that determine whether they win or lose the game. It was a fun project because it made programming feel more interactive and game-like.
The topics covered in today’s lesson include:
- Introducing Modulo
- Nested
ifStatements andelifStatements - BMI Calculator with Interpretations
- Multiple
ifStatements - Pizza Order Practice
- Logical Operators
- Final Project — Treasure Island
Let’s break them down one after the other.
Introducing Modulo
The modulo operator % is used to return the remainder after dividing two numbers.
Example:
print(10 % 3)
Output:
1
Why?
Because:
10 / 3 = 3 remainder 1
Modulo is commonly used when checking:
- Even and odd numbers
- Divisibility
- Counters and loops
Example:
number = 8
if number % 2 == 0:
print("Even number")
else:
print("Odd number")
Output:
Even number
Nested if Statements and elif Statements
An if statement allows us to make decisions in Python.
Example:
age = 18
if age >= 18:
print("You can vote")
Sometimes we want to check multiple conditions. That is where elif comes in.
Example:
score = 75
if score >= 90:
print("Excellent")
elif score >= 70:
print("Good")
else:
print("Try again")
Output:
Good
A nested if statement means placing one if statement inside another if statement.
Example:
age = 20
has_id = True
if age >= 18:
if has_id:
print("Access granted")
Nested conditions are useful when building games, login systems, and real-world applications.
Mini Project — BMI Calculator with Interpretations
In today’s lesson, we also built a BMI calculator that not only calculates BMI but also interprets the result.
BMI stands for Body Mass Index.
The formula is:
BMI = \frac{weight}{height^2}
Example:
weight = 70
height = 1.75
bmi = weight / (height ** 2)
print(bmi)
Output:
22.857142857142858
We can also interpret the result using conditional statements.
if bmi < 18.5:
print("Underweight")
elif bmi < 25:
print("Normal weight")
else:
print("Overweight")
Multiple if Statements
Unlike elif, multiple if statements allow Python to check every condition independently.
Example:
age = 25
if age > 18:
print("Adult")
if age > 21:
print("Can enter club")
Output:
Adult
Can enter club
Both conditions were checked because they are independent of each other.
Pizza Order Practice
Another fun exercise from today’s lesson was building a pizza order program.
The program calculates the total cost of pizza based on:
- Pizza size
- Extra toppings
- Cheese addition
Example:
bill = 0
size = "M"
if size == "S":
bill += 15
elif size == "M":
bill += 20
else:
bill += 25
print(bill)
Output:
20
This exercise helped me better understand how conditions work together in real-life scenarios.
Logical Operators
Logical operators are used to combine multiple conditions together.
Python has three major logical operators:
andornot
Example using and:
age = 20
has_id = True
if age >= 18 and has_id:
print("Access granted")
The condition only becomes True if both statements are true.
Example using or:
weather = "Rainy"
if weather == "Rainy" or weather == "Cloudy":
print("Take umbrella")
Example using not:
is_logged_in = False
if not is_logged_in:
print("Please login")
Logical operators are very important when building authentication systems, games, and applications with multiple conditions.
Final Project — Treasure Island
The final project for today was called Treasure Island.
The goal of the game is simple:
- Make the correct choices
- Avoid losing
- Find the treasure
This project combined everything learned today:
-
ifstatements elif- Nested conditions
- User input
- Decision-making
Here is the complete code for the project:
print('''
,adPPYb,d8 ,adPPYYba, 88,dPYba,,adPYba, ,adPPYba, ,adPPYba,
a8" `Y88 "" `Y8 88P' "88" "8a a8P_____88 I8[ ""
8b 88 ,adPPPPP88 88 88 88 8PP""""""" `"Y8ba,
"8a, ,d88 88, ,88 88 88 88 "8b, ,aa aa ]8I
`"YbbdP"Y8 `"8bbdP"Y8 88 88 88 `"Ybbd8"' `"YbbdP"'
aa, ,88
"Y8bbdP" ''')
print("Welcome to Treasure Island!")
print("Your mission is to find the treasure")
direction = input("Left or right? ")
if direction == "Right":
print("Game Over!")
elif direction == "Left":
step2 = input("Swim or Wait? ")
if step2 == "Swim":
print("Game Over!")
elif step2 == "Wait":
step3 = input("Which door? Red, Yellow, or Blue? ")
if step3 == "Red":
print("Game Over")
elif step3 == "Blue":
print("Game Over")
elif step3 == "Yellow":
print("You win!")
One thing I really enjoyed about this project was how a simple program could become interactive just by using conditions and user inputs.
This lesson made me understand how powerful decision-making is in programming because almost every real-world application depends on conditions to make decisions.
Top comments (0)