(Day 03) Task : Treasure Island Project :-


What You’ll Learn :_
This project focuses on:
Using
if
,elif
, andelse
statements.Nesting conditionals.
Applying logical operators (
and
,or
).Handling user input with
input()
.Creating a simple decision tree for gameplay.
The game challenges players to make choices at various crossroads, leading them toward the treasure or an untimely end. It's a fun way to practice decision-making logic in Python.
if
,else
Basics :-
These statements help your program make decisions.
if Condition:
Do this
else:
Do this
if
checks the condition.else
runs if theif
condition is false.Now :- we’ll elaborate the if & else (conditional) statements.
Conditionals in python are used to check a condition and execute specific lines of code based on whether the condition is True or False.
Condition Check :-
In Python to check a conditions and tell the computer what to do in each case.
if <this condition is true>:
<then execute this line of code>
age = 18 if age >= 18: print("You Can vote")
What if the condition is false?
The else keyword is used to define a block of code that will execute if the condition checked in the if statement is false.
if <this condition is true>:
<then don’t execute this line of code>
else:
<then execute this line of code>.
if pigs can fly: <Some code that will never execute> else: print("This is real life")
Note :- Python Indentation ( Python uses indentation to denote hierarchichal relationships between lines of code such as parent and child lines).
Example :
Comparator Operators :-
Comparison operators are used to compare two values. They always return a Boolean value — either
True
orFalse
.✅ Common Python Comparison Operators:
==
→ Equal to :-
Checks if two values are equal.
Example:5 == 5
→True
.!=
→ Not equal to :-
Checks if two values are not equal.
Example:5 != 3
→True
.>
→ Greater than :-
Checks if the left value is greater than the right.
Example:10 > 7
→True
.<
→ Less than :-
Checks if the left value is less than the right.
Example:3 < 8
→True
.>=
→ Greater than or equal to :-
True if the left value is greater than or equal to the right.
Example:5 >= 5
→True
.<=
→ Less than or equal to :-
True if the left value is less than or equal to the right.
Example:2 <= 3
→True
.
What is the Modulo Operator?
The modulo operator
%
gives you the remainder after dividing one number by another.It's like asking: “What’s left over?” after you divide two numbers.
7 % 3 # output will be 1 6 % 2 # output will be 0 6 % 5 # output will be 1 6 % 4 # output will be 2
Write some code using what you have learnt about the modulo operator and conditional checks in Python to check if the number in he input area is odd or even. If it's odd print out the word "Odd" otherwise printout "Even".
Understanding
elif
and Nestedif
Statements in Python :
What is elif
?
elif
stands for “else if”.It's used when you want to check multiple conditions, one after another.
It comes after an
if
and before anelse
.Many elif conditions can come between if and else statement.
age = 17 if age > 18: print("Adult") elif age == 18: print("Just turned adult!") else: print("Minor")
The program first checks:
Is age > 18?
→ No.Then it checks:
Is age == 18?
→ No.So, it goes to the
else
and prints: "Minor".
What is Nesting (Nested if
Statements)?
Nesting means putting one
if
inside another.It’s used when a decision depends on another decision.
age = 20 has_id = True if age >= 18: if has_id: print("You can enter.") else: print("ID required.") else: print("You're too young.")
First, it checks:
Is age >= 18?
If yes, then it goes inside and checks:
Do you have ID?
If either condition fails, you get the right message.
Build an automatic pizza order program :-
Based on a user's order, work out their final bill. Use the input() function to get a user's preferences and then add up the total for their order and tell them how much they have to pay.
Small pizza (S): $15
Medium pizza (M): $20
Large pizza (L): $25
Add pepperoni for small pizza (Y or N): +$2
Add pepperoni for medium or large pizza (Y or N): +$3
Add extra cheese for any size pizza (Y or N): +$1
Treasure Island Project :-
Flow Chart :
Subscribe to my newsletter
Read articles from Aditya Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Aditya Sharma
Aditya Sharma
DevOps Enthusiast | Python | Chef | Docker | GitHub | Linux | Shell Scripting | CI/CD & Cloud Learner | AWS