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
148 changes: 144 additions & 4 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,151 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"🏰 Welcome to the Haunted Mansion!\n",
"Your goal: find the treasure without dying.\n",
"\n",
"You find a dusty chest...\n",
"You found a potion!\n",
"Your health: 10\n",
"You walk into a dark corridor...\n",
"\n",
"👻 A ghost appears!\n",
"You defeated the ghost!\n",
"You found a sword!\n",
"Your health: 10\n",
"You walk into a dark corridor...\n",
"\n",
"👻 A ghost appears!\n",
"You slash the ghost with your sword!\n",
"You defeated the ghost!\n",
"You found a potion!\n",
"Your health: 10\n",
"You walk into a dark corridor...\n",
"\n",
"👻 A ghost appears!\n",
"You slash the ghost with your sword!\n",
"You defeated the ghost!\n",
"You found a sword!\n",
"Your health: 10\n",
"You find a dusty chest...\n",
"You found a key!\n",
"Your health: 10\n",
"You find a dusty chest...\n",
"You found a potion!\n",
"Your health: 10\n",
"You find a dusty chest...\n",
"You found a potion!\n",
"Your health: 10\n",
"You walk into a dark corridor...\n",
"\n",
"👻 A ghost appears!\n",
"You slash the ghost with your sword!\n",
"You defeated the ghost!\n",
"You found a key!\n",
"\n",
"🎉 You found the hidden treasure and escaped the mansion alive!\n",
"Final health: 10\n",
"Your items: ['potion', 'sword', 'potion', 'sword', 'key', 'potion', 'potion', 'key']\n",
"🏰 Welcome to the Haunted Mansion!\n",
"Your goal: find the treasure without dying.\n",
"\n",
"You walk into a dark corridor...\n",
"\n",
"👻 A ghost appears!\n",
"You defeated the ghost!\n",
"You found a potion!\n",
"\n",
"🎉 You found the hidden treasure and escaped the mansion alive!\n",
"Final health: 10\n",
"Your items: ['potion']\n"
]
}
],
"source": [
"import random\n",
"\n",
"# Items player can collect\n",
"items = [\"sword\", \"potion\", \"key\", \"shield\"]\n",
"\n",
"def encounter_ghost(inventory, health):\n",
" \"\"\"Simulate a ghost encounter. Return updated health + inventory.\"\"\"\n",
" print(\"\\n👻 A ghost appears!\")\n",
"\n",
" # Random ghost strength\n",
" ghost_strength = random.randint(1, 10)\n",
"\n",
" # If player has sword, reduce ghost strength\n",
" if \"sword\" in inventory:\n",
" print(\"You slash the ghost with your sword!\")\n",
" ghost_strength -= 3\n",
"\n",
" # If player has potion, use it to heal\n",
" if \"potion\" in inventory and health < 5:\n",
" print(\"You drink a potion and regain 3 health.\")\n",
" health += 3\n",
" inventory.remove(\"potion\")\n",
"\n",
" # Outcome of fight\n",
" if ghost_strength > 5:\n",
" print(\"The ghost was strong! You lose 3 health.\")\n",
" health -= 3\n",
" else:\n",
" print(\"You defeated the ghost!\")\n",
" # Chance to drop an item\n",
" new_item = random.choice(items)\n",
" print(f\"You found a {new_item}!\")\n",
" inventory.append(new_item)\n",
"\n",
" return inventory, health\n",
"\n",
"\n",
"def run_mansion():\n",
" \"\"\"Main game loop.\"\"\"\n",
" health = 10\n",
" inventory = []\n",
" print(\"🏰 Welcome to the Haunted Mansion!\")\n",
" print(\"Your goal: find the treasure without dying.\\n\")\n",
"\n",
" while health > 0:\n",
" # Random path choice\n",
" choice = input(\"You reach an intersection. Go left or right? \").lower()\n",
"\n",
" if choice == \"left\":\n",
" print(\"You walk into a dark corridor...\")\n",
" inventory, health = encounter_ghost(inventory, health)\n",
"\n",
" elif choice == \"right\":\n",
" print(\"You find a dusty chest...\")\n",
" found_item = random.choice(items)\n",
" print(f\"You found a {found_item}!\")\n",
" inventory.append(found_item)\n",
"\n",
" # Random chance to find treasure\n",
" if random.random() < 0.2:\n",
" print(\"\\n🎉 You found the hidden treasure and escaped the mansion alive!\")\n",
" print(\"Final health:\", health)\n",
" print(\"Your items:\", inventory)\n",
" return\n",
"\n",
" # Check health\n",
" print(f\"Your health: {health}\")\n",
" if health <= 0:\n",
" print(\"\\n💀 You collapsed... The ghost wins!\")\n",
" return\n",
"\n",
"\n",
"# Run the game\n",
"run_mansion()\n",
"\n",
"\n",
"run_mansion()"
]
},
Expand All @@ -162,7 +302,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +316,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down