From 3175fa9ceb834548369ca33f763d5780ee7f741b Mon Sep 17 00:00:00 2001 From: slmj1990-ai Date: Thu, 19 Feb 2026 16:13:12 +0100 Subject: [PATCH] lab_extra --- lab-python-flow-control.ipynb | 233 +++++++++++++++++++++++++++++++++- 1 file changed, 229 insertions(+), 4 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..e83c1a0 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -77,7 +77,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "499552c8-9e30-46e1-a706-4ac5dc64670e", "metadata": {}, "outputs": [], @@ -98,7 +98,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, "outputs": [], @@ -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" }, @@ -176,7 +401,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.2" } }, "nbformat": 4,