The Checkpoint
You reach the Inner Sanctum. The guards are very strict. They check your ID first, and then ask for a password.
This is a Nested If. An if inside another if.
Nested Spells
has_id = True
password = "123"
if has_id:
print("ID Checked.")
if password == "123":
print("Welcome in!")
else:
print("Wrong password!")
else:
print("No ID, go away!")
Notice the indentation increases! We are now two levels deep.
Your Mission
1
First Gate
Create a variable is_member set to True.
2
Second Gate
Create a variable age set to 10.
3
The Check
Write an if to check is_member.
4
Inner Check
Inside the first if, write another if to check if age > 12.
5
Outcome
Inside the inner if, print "Full Access". Add an else to the inner if to print "Junior Access".
Suggested SolutionClick to expandClick to collapse
Here is the nested logic:
is_member = True
age = 10
if is_member:
if age > 12:
print("Full Access")
else:
print("Junior Access")Advanced TipsWant more? Click to expandClick to collapse
Too many nested ifs can be hard to read (we call it "Spaghetti Code"). Sometimes using and is cleaner!
if is_member and age > 12:
print("Full Access")pymain.py
Loading...
Terminal
Terminal
Ready to run...