Back

Activity 1.12: Nested Conditionals

divider

Activity 1.12

Nested Conditionals

Key Concepts

Nesting

pass

Nesting

An if statement can go inside another if statement's block. This is called nesting — the inner decision only happens if the outer condition was true first.

answer = input("Would you like to delete your account?")
if answer == "yes":
confirm = input("Are you ABSOLUTELY SURE?")
if confirm == "yes":
print("Deleting your account.")
else:
print("Cancelling your request.")

Nesting

Terminal window
Would you like to delete your account? yes [Enter]
Are you ABSOLUTELY SURE? yes [Enter]
Deleting your account.

pass

Python can't have a truly empty indented block — if you want to sketch out a structure before filling it in, pass is a placeholder that does nothing.

if path == "left":
pass
elif path == "right":
pass
else:
pass

Today's Project: Dungeon Crawler

A choose-your-own-adventure story, built one branch at a time — starting with an empty skeleton of nested if/elif/else blocks, filled in one path at a time.

Today's Objectives

  • Nesting an if statement inside another
  • Sketching a decision structure with pass before filling it in
  • Tracking a story's state with boolean and numeric variables

Key Terms

Nesting
Placing one code block (like an if) inside another.
pass
A placeholder statement that does nothing — used where Python requires a block but you have nothing to put there yet.

'F' → Fullscreen

divider

Build

Create a new Python program named 1-12-dungeon-crawler. You'll build this story one branch at a time.


Task 1: Set Up Your Variables

  • Declare the ending flags, gold, and twinkies — all start false or zero.
Nested Conditionals
# If the player beats one of the levels, set its boolean variable to True
wolfpack_ending = False
dragon_ending = False
gold = 0
twinkies = 0

Task 2: The Prologue

  • Set the scene, then ask the player which path they take.
  • If they chose a real path, offer them the Twinkie.
Nested Conditionals
# If the player beats one of the levels, set its boolean variable to True
wolfpack_ending = False
dragon_ending = False
gold = 0
twinkies = 0
# Prologue
print("Your car crashes into a tree during a thunderstorm in the woods.")
print("Darkness surrounds you as thunder crashes and rain pours.")
print("You spot a cave nearby, dimly lit by flashes of lightning, and run towards it for shelter.")
print("Inside, you see two shadowy paths diverging deeper into the unknown...")
path = input("Which path do you take? (left or right) ->").lower()
if path == "left" or path == "right":
print("As you move forward, you see something glimmering on the ground - a Hostess Twinkie.")
print("Do you eat the Twinkie for sustenance, or will you save it for later?")
twinkie_choice = input("(take or eat) ->").lower()
if twinkie_choice == "take":
print("You slip the Twinkie into your pocket, unsure what awaits in the darkness...")
twinkies = twinkies + 1
elif twinkie_choice == "eat":
print("Feeling uneasy, you eat the Twinkie to keep up your strength.")
else:
print("You leave the Twinkie untouched, feeling an ominous presence watching.")

Task 3: Sketch the Cave Branches

  • Add an if/elif/else for the left path, right path, and no-path-chosen case — using pass to leave each one empty for now.
Nested Conditionals
# If the player beats one of the levels, set its boolean variable to True
wolfpack_ending = False
dragon_ending = False
gold = 0
twinkies = 0
# Prologue
print("Your car crashes into a tree during a thunderstorm in the woods.")
print("Darkness surrounds you as thunder crashes and rain pours.")
print("You spot a cave nearby, dimly lit by flashes of lightning, and run towards it for shelter.")
print("Inside, you see two shadowy paths diverging deeper into the unknown...")
path = input("Which path do you take? (left or right) ->").lower()
if path == "left" or path == "right":
print("As you move forward, you see something glimmering on the ground - a Hostess Twinkie.")
print("Do you eat the Twinkie for sustenance, or will you save it for later?")
twinkie_choice = input("(take or eat) ->").lower()
if twinkie_choice == "take":
print("You slip the Twinkie into your pocket, unsure what awaits in the darkness...")
twinkies = twinkies + 1
elif twinkie_choice == "eat":
print("Feeling uneasy, you eat the Twinkie to keep up your strength.")
else:
print("You leave the Twinkie untouched, feeling an ominous presence watching.")
# Caves
# Left -> Wolfpack
# Right -> Dragon
# Else -> No path chosen
if path == "left":
pass
elif path == "right":
pass
else:
pass

Task 4: The Wolfpack Path

  • Fill in the left path — the player meets a wolf pack, and the Twinkie changes the outcome.
Nested Conditionals
# If the player beats one of the levels, set its boolean variable to True
wolfpack_ending = False
dragon_ending = False
gold = 0
twinkies = 0
# Prologue
print("Your car crashes into a tree during a thunderstorm in the woods.")
print("Darkness surrounds you as thunder crashes and rain pours.")
print("You spot a cave nearby, dimly lit by flashes of lightning, and run towards it for shelter.")
print("Inside, you see two shadowy paths diverging deeper into the unknown...")
path = input("Which path do you take? (left or right) ->").lower()
if path == "left" or path == "right":
print("As you move forward, you see something glimmering on the ground - a Hostess Twinkie.")
print("Do you eat the Twinkie for sustenance, or will you save it for later?")
twinkie_choice = input("(take or eat) ->").lower()
if twinkie_choice == "take":
print("You slip the Twinkie into your pocket, unsure what awaits in the darkness...")
twinkies = twinkies + 1
elif twinkie_choice == "eat":
print("Feeling uneasy, you eat the Twinkie to keep up your strength.")
else:
print("You leave the Twinkie untouched, feeling an ominous presence watching.")
# Caves
# Left -> Wolfpack
# Right -> Dragon
# Else -> No path chosen
if path == "left":
print("You cautiously take the left path.")
print("A few steps in, you freeze as the piercing eyes of a pack of wolves emerge from the shadows.")
print("The pack leader, larger and fiercer than the others, steps forward, growling softly...")
wolf_choice = input("Do you offer the pack leader your hand in peace? (yes or no) ->").lower()
if wolf_choice == "yes" and twinkies >= 1:
print("As you reach out, the wolves smell the Twinkie...")
print("In a frenzy of hunger, they turn on you, and you're surrounded with no escape!")
elif wolf_choice == "yes":
print("The pack leader sniffs your hand and accepts you into the fold.")
print("You're given a small satchel of gold as a token of their trust.")
print("You settle in with the wolves, awaiting dawn as the storm howls outside...")
gold = gold + 5
wolfpack_ending = True
else:
print("Sensing your hesitation, the wolves close in, their teeth bared.")
print("You feel their claws sink in, and everything goes black...")
elif path == "right":
pass
else:
pass

Task 5: The Dragon Path

  • Fill in the right path — the player meets a dragon and can choose how much gold to take.
Nested Conditionals
# If the player beats one of the levels, set its boolean variable to True
wolfpack_ending = False
dragon_ending = False
gold = 0
twinkies = 0
# Prologue
print("Your car crashes into a tree during a thunderstorm in the woods.")
print("Darkness surrounds you as thunder crashes and rain pours.")
print("You spot a cave nearby, dimly lit by flashes of lightning, and run towards it for shelter.")
print("Inside, you see two shadowy paths diverging deeper into the unknown...")
path = input("Which path do you take? (left or right) ->").lower()
if path == "left" or path == "right":
print("As you move forward, you see something glimmering on the ground - a Hostess Twinkie.")
print("Do you eat the Twinkie for sustenance, or will you save it for later?")
twinkie_choice = input("(take or eat) ->").lower()
if twinkie_choice == "take":
print("You slip the Twinkie into your pocket, unsure what awaits in the darkness...")
twinkies = twinkies + 1
elif twinkie_choice == "eat":
print("Feeling uneasy, you eat the Twinkie to keep up your strength.")
else:
print("You leave the Twinkie untouched, feeling an ominous presence watching.")
# Caves
# Left -> Wolfpack
# Right -> Dragon
# Else -> No path chosen
if path == "left":
print("You cautiously take the left path.")
print("A few steps in, you freeze as the piercing eyes of a pack of wolves emerge from the shadows.")
print("The pack leader, larger and fiercer than the others, steps forward, growling softly...")
wolf_choice = input("Do you offer the pack leader your hand in peace? (yes or no) ->").lower()
if wolf_choice == "yes" and twinkies >= 1:
print("As you reach out, the wolves smell the Twinkie...")
print("In a frenzy of hunger, they turn on you, and you're surrounded with no escape!")
elif wolf_choice == "yes":
print("The pack leader sniffs your hand and accepts you into the fold.")
print("You're given a small satchel of gold as a token of their trust.")
print("You settle in with the wolves, awaiting dawn as the storm howls outside...")
gold = gold + 5
wolfpack_ending = True
else:
print("Sensing your hesitation, the wolves close in, their teeth bared.")
print("You feel their claws sink in, and everything goes black...")
elif path == "right":
print("You take the right path, deeper into the damp, dark cave.")
print("Suddenly, a rotten, smoky smell fills the air. A massive dragon with dull, greedy eyes looms in the shadows.")
print("The dragon sniffs the air and sneers, 'Did you bring me something sweet?'")
dragon_choice = input("Do you offer him a treat? (yes or no) ->").lower()
if dragon_choice == "yes" and twinkies >= 1:
print("The dragon's eyes widen as he devours the Twinkie. 'Delicious!' he roars.")
print("In gratitude, he hands you a pile of gold coins.")
gold_choice = input("How much gold do you take? ->")
gold_amount = int(gold_choice)
if gold_amount > 0 and gold_amount <= 100:
print("The dragon grunts approval, allowing you to take the gold...")
gold = gold + gold_amount
elif gold_amount > 100:
print("The dragon's eyes narrow, and he hisses, 'Greed will get you nothing.'")
else:
print("The dragon, impressed by your modesty, lets you take all the gold!")
gold = gold + 100000
dragon_ending = True
else:
print("The dragon's face darkens, and he lunges towards you!")
print("His jaws close around you as everything fades to black...")
else:
pass

Task 6: No Path Chosen

  • Fill in what happens if the player never picked left or right.
Nested Conditionals
# If the player beats one of the levels, set its boolean variable to True
wolfpack_ending = False
dragon_ending = False
gold = 0
twinkies = 0
# Prologue
print("Your car crashes into a tree during a thunderstorm in the woods.")
print("Darkness surrounds you as thunder crashes and rain pours.")
print("You spot a cave nearby, dimly lit by flashes of lightning, and run towards it for shelter.")
print("Inside, you see two shadowy paths diverging deeper into the unknown...")
path = input("Which path do you take? (left or right) ->").lower()
if path == "left" or path == "right":
print("As you move forward, you see something glimmering on the ground - a Hostess Twinkie.")
print("Do you eat the Twinkie for sustenance, or will you save it for later?")
twinkie_choice = input("(take or eat) ->").lower()
if twinkie_choice == "take":
print("You slip the Twinkie into your pocket, unsure what awaits in the darkness...")
twinkies = twinkies + 1
elif twinkie_choice == "eat":
print("Feeling uneasy, you eat the Twinkie to keep up your strength.")
else:
print("You leave the Twinkie untouched, feeling an ominous presence watching.")
# Caves
# Left -> Wolfpack
# Right -> Dragon
# Else -> No path chosen
if path == "left":
print("You cautiously take the left path.")
print("A few steps in, you freeze as the piercing eyes of a pack of wolves emerge from the shadows.")
print("The pack leader, larger and fiercer than the others, steps forward, growling softly...")
wolf_choice = input("Do you offer the pack leader your hand in peace? (yes or no) ->").lower()
if wolf_choice == "yes" and twinkies >= 1:
print("As you reach out, the wolves smell the Twinkie...")
print("In a frenzy of hunger, they turn on you, and you're surrounded with no escape!")
elif wolf_choice == "yes":
print("The pack leader sniffs your hand and accepts you into the fold.")
print("You're given a small satchel of gold as a token of their trust.")
print("You settle in with the wolves, awaiting dawn as the storm howls outside...")
gold = gold + 5
wolfpack_ending = True
else:
print("Sensing your hesitation, the wolves close in, their teeth bared.")
print("You feel their claws sink in, and everything goes black...")
elif path == "right":
print("You take the right path, deeper into the damp, dark cave.")
print("Suddenly, a rotten, smoky smell fills the air. A massive dragon with dull, greedy eyes looms in the shadows.")
print("The dragon sniffs the air and sneers, 'Did you bring me something sweet?'")
dragon_choice = input("Do you offer him a treat? (yes or no) ->").lower()
if dragon_choice == "yes" and twinkies >= 1:
print("The dragon's eyes widen as he devours the Twinkie. 'Delicious!' he roars.")
print("In gratitude, he hands you a pile of gold coins.")
gold_choice = input("How much gold do you take? ->")
gold_amount = int(gold_choice)
if gold_amount > 0 and gold_amount <= 100:
print("The dragon grunts approval, allowing you to take the gold...")
gold = gold + gold_amount
elif gold_amount > 100:
print("The dragon's eyes narrow, and he hisses, 'Greed will get you nothing.'")
else:
print("The dragon, impressed by your modesty, lets you take all the gold!")
gold = gold + 100000
dragon_ending = True
else:
print("The dragon's face darkens, and he lunges towards you!")
print("His jaws close around you as everything fades to black...")
else:
print("You hesitate, feeling the storm grow stronger as lightning flashes around you.")
print("Suddenly, a bolt strikes nearby, and you fall to the ground, darkness closing in.")

Task 7: The Epilogue

  • Print an ending based on which boolean flags got set to True.
Nested Conditionals
# If the player beats one of the levels, set its boolean variable to True
wolfpack_ending = False
dragon_ending = False
gold = 0
twinkies = 0
# Prologue
print("Your car crashes into a tree during a thunderstorm in the woods.")
print("Darkness surrounds you as thunder crashes and rain pours.")
print("You spot a cave nearby, dimly lit by flashes of lightning, and run towards it for shelter.")
print("Inside, you see two shadowy paths diverging deeper into the unknown...")
path = input("Which path do you take? (left or right) ->").lower()
if path == "left" or path == "right":
print("As you move forward, you see something glimmering on the ground - a Hostess Twinkie.")
print("Do you eat the Twinkie for sustenance, or will you save it for later?")
twinkie_choice = input("(take or eat) ->").lower()
if twinkie_choice == "take":
print("You slip the Twinkie into your pocket, unsure what awaits in the darkness...")
twinkies = twinkies + 1
elif twinkie_choice == "eat":
print("Feeling uneasy, you eat the Twinkie to keep up your strength.")
else:
print("You leave the Twinkie untouched, feeling an ominous presence watching.")
# Caves
# Left -> Wolfpack
# Right -> Dragon
# Else -> No path chosen
if path == "left":
print("You cautiously take the left path.")
print("A few steps in, you freeze as the piercing eyes of a pack of wolves emerge from the shadows.")
print("The pack leader, larger and fiercer than the others, steps forward, growling softly...")
wolf_choice = input("Do you offer the pack leader your hand in peace? (yes or no) ->").lower()
if wolf_choice == "yes" and twinkies >= 1:
print("As you reach out, the wolves smell the Twinkie...")
print("In a frenzy of hunger, they turn on you, and you're surrounded with no escape!")
elif wolf_choice == "yes":
print("The pack leader sniffs your hand and accepts you into the fold.")
print("You're given a small satchel of gold as a token of their trust.")
print("You settle in with the wolves, awaiting dawn as the storm howls outside...")
gold = gold + 5
wolfpack_ending = True
else:
print("Sensing your hesitation, the wolves close in, their teeth bared.")
print("You feel their claws sink in, and everything goes black...")
elif path == "right":
print("You take the right path, deeper into the damp, dark cave.")
print("Suddenly, a rotten, smoky smell fills the air. A massive dragon with dull, greedy eyes looms in the shadows.")
print("The dragon sniffs the air and sneers, 'Did you bring me something sweet?'")
dragon_choice = input("Do you offer him a treat? (yes or no) ->").lower()
if dragon_choice == "yes" and twinkies >= 1:
print("The dragon's eyes widen as he devours the Twinkie. 'Delicious!' he roars.")
print("In gratitude, he hands you a pile of gold coins.")
gold_choice = input("How much gold do you take? ->")
gold_amount = int(gold_choice)
if gold_amount > 0 and gold_amount <= 100:
print("The dragon grunts approval, allowing you to take the gold...")
gold = gold + gold_amount
elif gold_amount > 100:
print("The dragon's eyes narrow, and he hisses, 'Greed will get you nothing.'")
else:
print("The dragon, impressed by your modesty, lets you take all the gold!")
gold = gold + 100000
dragon_ending = True
else:
print("The dragon's face darkens, and he lunges towards you!")
print("His jaws close around you as everything fades to black...")
else:
print("You hesitate, feeling the storm grow stronger as lightning flashes around you.")
print("Suddenly, a bolt strikes nearby, and you fall to the ground, darkness closing in.")
# Epilogue
print("-- Ending --")
if wolfpack_ending:
print("By cuddling with the wolfpack, you weather out the storm.")
print("Upon leaving the cave, they follow you, seeing you as the new pack leader.")
print("You survived!!!")
elif dragon_ending:
print("By tempting the dragon with a delicious Hostess snack cake, you avoid his wrath.")
print("He flies you back home after the storm, but not after raiding the nearest Hostess factory.")
print("You survived!!!")
else:
print("Game Over!")
if gold > 0:
print(f"You not only survived, but you found {gold} gold!")
divider

Checkpoint

Verify your program works correctly.

Example Output
Your car crashes into a tree during a thunderstorm in the woods.
Darkness surrounds you as thunder crashes and rain pours.
You spot a cave nearby, dimly lit by flashes of lightning, and run towards it for shelter.
Inside, you see two shadowy paths diverging deeper into the unknown...
Which path do you take? (left or right) ->left [Enter]
As you move forward, you see something glimmering on the ground - a Hostess Twinkie.
Do you eat the Twinkie for sustenance, or will you save it for later?
(take or eat) ->take [Enter]
You slip the Twinkie into your pocket, unsure what awaits in the darkness...
You cautiously take the left path.
A few steps in, you freeze as the piercing eyes of a pack of wolves emerge from the shadows.
The pack leader, larger and fiercer than the others, steps forward, growling softly...
Do you offer the pack leader your hand in peace? (yes or no) ->yes [Enter]
As you reach out, the wolves smell the Twinkie...
In a frenzy of hunger, they turn on you, and you're surrounded with no escape!
-- Ending --
Game Over!
divider

Reflection

Answer the following questions before submitting your work.

  1. Why did the wolf pack and dragon branches need to be nested inside the left/right path check, instead of being separate standalone if statements?
  2. What would have happened if you tried to leave the cave branches truly empty in Task 3, without pass?
  3. How do the two ending boolean flags decide which epilogue message prints?
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete