Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
233 changes: 229 additions & 4 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "499552c8-9e30-46e1-a706-4ac5dc64670e",
"metadata": {},
"outputs": [],
Expand All @@ -98,7 +98,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -158,11 +158,236 @@
"source": [
"This should print the game's narrative and prompt the user to make choices and fight ghosts. The game ends when the adventurer finds the key or loses all their health points. "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e554ae65",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"🏚️ Welcome to the Haunted Mansion!\n",
"Your goal is to find the hidden treasure.\n",
"\n",
"You arrive at an intersection.\n",
"Choose a path:\n",
"1. Dark Hallway\n",
"2. Creaky Stairs\n",
"3. Dusty Library\n",
"⚠️ A trap triggers!\n",
"You take 12 damage!\n",
"Invalid path.\n",
"\n",
"Rooms explored: 1/5\n",
"Current Health: 88\n",
"Inventory: []\n",
"\n",
"You arrive at an intersection.\n",
"Choose a path:\n",
"1. Dark Hallway\n",
"2. Creaky Stairs\n",
"3. Dusty Library\n",
"⚠️ A trap triggers!\n",
"You take 8 damage!\n",
"Invalid path.\n",
"\n",
"Rooms explored: 2/5\n",
"Current Health: 80\n",
"Inventory: []\n",
"\n",
"You arrive at an intersection.\n",
"Choose a path:\n",
"1. Dark Hallway\n",
"2. Creaky Stairs\n",
"3. Dusty Library\n",
"Invalid path.\n",
"\n",
"Rooms explored: 3/5\n",
"Current Health: 80\n",
"Inventory: []\n",
"\n",
"You arrive at an intersection.\n",
"Choose a path:\n",
"1. Dark Hallway\n",
"2. Creaky Stairs\n",
"3. Dusty Library\n",
"Invalid path.\n",
"\n",
"Rooms explored: 4/5\n",
"Current Health: 80\n",
"Inventory: []\n",
"\n",
"You arrive at an intersection.\n",
"Choose a path:\n",
"1. Dark Hallway\n",
"2. Creaky Stairs\n",
"3. Dusty Library\n",
"⚠️ A trap triggers!\n",
"You take 8 damage!\n",
"Invalid path.\n",
"\n",
"Rooms explored: 5/5\n",
"Current Health: 72\n",
"Inventory: []\n",
"\n",
"🏆 You reach the final chamber...\n",
"Inside, you find the legendary treasure chest!\n",
"💰 Congratulations! You survived the Haunted Mansion!\n"
]
}
],
"source": [
"import random\n",
"\n",
"# List of possible items\n",
"ITEMS = [\"Sword\", \"Shield\", \"Health Potion\", \"Magic Key\", \"Lantern\"]\n",
"\n",
"def encounter_ghost(player):\n",
" print(\"\\n👻 A ghost appears from the shadows!\")\n",
" \n",
" ghost_health = random.randint(20, 40)\n",
"\n",
" while ghost_health > 0 and player[\"health\"] > 0:\n",
" print(f\"\\nYour Health: {player['health']}\")\n",
" print(f\"Ghost Health: {ghost_health}\")\n",
" print(\"Choose an action:\")\n",
" print(\"1. Attack\")\n",
" print(\"2. Use Item\")\n",
" print(\"3. Run\")\n",
"\n",
" choice = input(\"> \")\n",
"\n",
" if choice == \"1\":\n",
" damage = random.randint(5, 15)\n",
" if \"Sword\" in player[\"inventory\"]:\n",
" damage += 5\n",
" print(\"⚔️ Your sword increases your damage!\")\n",
" ghost_health -= damage\n",
" print(f\"You deal {damage} damage to the ghost!\")\n",
"\n",
" elif choice == \"2\":\n",
" if not player[\"inventory\"]:\n",
" print(\"You have no items!\")\n",
" else:\n",
" print(\"Inventory:\", player[\"inventory\"])\n",
" item = input(\"Which item do you want to use? \")\n",
"\n",
" if item == \"Health Potion\" and \"Health Potion\" in player[\"inventory\"]:\n",
" player[\"health\"] += 20\n",
" player[\"inventory\"].remove(\"Health Potion\")\n",
" print(\"🧪 You restored 20 health!\")\n",
" elif item == \"Shield\" and \"Shield\" in player[\"inventory\"]:\n",
" print(\"🛡️ Shield reduces incoming damage this turn!\")\n",
" reduced = random.randint(1, 5)\n",
" player[\"health\"] -= reduced\n",
" print(f\"Ghost deals only {reduced} damage!\")\n",
" else:\n",
" print(\"Invalid item choice.\")\n",
"\n",
" elif choice == \"3\":\n",
" escape = random.choice([True, False])\n",
" if escape:\n",
" print(\"🏃 You escaped successfully!\")\n",
" return\n",
" else:\n",
" print(\"You failed to escape!\")\n",
"\n",
" else:\n",
" print(\"Invalid choice!\")\n",
"\n",
" if ghost_health > 0:\n",
" ghost_damage = random.randint(5, 12)\n",
" if \"Shield\" in player[\"inventory\"]:\n",
" ghost_damage -= 2\n",
" player[\"health\"] -= max(ghost_damage, 0)\n",
" print(f\"The ghost hits you for {ghost_damage} damage!\")\n",
"\n",
" if player[\"health\"] <= 0:\n",
" print(\"💀 The ghost has defeated you...\")\n",
" else:\n",
" print(\"✨ You defeated the ghost!\")\n",
"\n",
"\n",
"def run_mansion():\n",
" player = {\n",
" \"health\": 100,\n",
" \"inventory\": []\n",
" }\n",
"\n",
" print(\"🏚️ Welcome to the Haunted Mansion!\")\n",
" print(\"Your goal is to find the hidden treasure.\")\n",
"\n",
" rooms_cleared = 0\n",
"\n",
" while player[\"health\"] > 0 and rooms_cleared < 5:\n",
" print(\"\\nYou arrive at an intersection.\")\n",
" print(\"Choose a path:\")\n",
" print(\"1. Dark Hallway\")\n",
" print(\"2. Creaky Stairs\")\n",
" print(\"3. Dusty Library\")\n",
"\n",
" choice = input(\"> \")\n",
"\n",
" # Random event chance\n",
" event_chance = random.randint(1, 100)\n",
"\n",
" if event_chance <= 30:\n",
" found_item = random.choice(ITEMS)\n",
" print(f\"🎁 You found a {found_item}!\")\n",
" player[\"inventory\"].append(found_item)\n",
"\n",
" elif event_chance <= 60:\n",
" print(\"⚠️ A trap triggers!\")\n",
" trap_damage = random.randint(5, 15)\n",
" player[\"health\"] -= trap_damage\n",
" print(f\"You take {trap_damage} damage!\")\n",
"\n",
" # Path logic\n",
" if choice == \"1\":\n",
" print(\"You walk down the dark hallway...\")\n",
" encounter_ghost(player)\n",
"\n",
" elif choice == \"2\":\n",
" print(\"You climb the creaky stairs...\")\n",
" for step in range(3):\n",
" print(f\"Step {step+1} creaks loudly...\")\n",
" encounter_ghost(player)\n",
"\n",
" elif choice == \"3\":\n",
" print(\"You explore the dusty library...\")\n",
" if \"Magic Key\" in player[\"inventory\"]:\n",
" print(\"🔑 You unlock a hidden chest and gain 30 health!\")\n",
" player[\"health\"] += 30\n",
" else:\n",
" encounter_ghost(player)\n",
"\n",
" else:\n",
" print(\"Invalid path.\")\n",
"\n",
" rooms_cleared += 1\n",
" print(f\"\\nRooms explored: {rooms_cleared}/5\")\n",
" print(f\"Current Health: {player['health']}\")\n",
" print(f\"Inventory: {player['inventory']}\")\n",
"\n",
" if player[\"health\"] > 0:\n",
" print(\"\\n🏆 You reach the final chamber...\")\n",
" print(\"Inside, you find the legendary treasure chest!\")\n",
" print(\"💰 Congratulations! You survived the Haunted Mansion!\")\n",
" else:\n",
" print(\"\\n💀 You have perished in the Haunted Mansion...\")\n",
"\n",
"\n",
"# Start the game\n",
"run_mansion()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +401,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.2"
}
},
"nbformat": 4,
Expand Down