From a36944467257e8841ff0b193eeb2f60c26b5e1ff Mon Sep 17 00:00:00 2001 From: PabloXberg Date: Fri, 20 Feb 2026 16:56:38 +0100 Subject: [PATCH] Extra Lab 3 - Solved --- lab-python-flow-control.ipynb | 242 +++++++++++++++++++++++++++++++++- 1 file changed, 235 insertions(+), 7 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..c938efb 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": [], @@ -93,7 +93,57 @@ " \"\"\"\n", " print(\"You encounter a ghost!\")\n", " \n", - " # your code goes here" + "\n", + "# Items the adventurer can find along the way\n", + "ITEM_POOL = [\"potion\", \"bandage\", \"holy_water\", \"silver_dagger\"]\n", + "\n", + "\n", + "def encounter_ghost(items=None):\n", + " \"\"\"\n", + " Handles the encounter with a ghost.\n", + "\n", + " A random number between 1 and 10 is rolled to decide the outcome.\n", + " - If the (final) number is <= 5, the adventurer defeats the ghost.\n", + " - If the (final) number is > 5, the adventurer loses.\n", + "\n", + " Items can improve your odds by lowering the roll (still random, just influenced).\n", + " Returns:\n", + " True -> adventurer defeated the ghost\n", + " False -> ghost defeated the adventurer\n", + " \"\"\"\n", + " if items is None:\n", + " items = []\n", + "\n", + " print(\"\\nYou encounter a ghost!\")\n", + "\n", + " bonus = 0\n", + "\n", + " # Let the player use items (if they have them) to improve their odds\n", + " if \"holy_water\" in items:\n", + " choice = input(\"Use holy_water to weaken the ghost? (y/n): \").strip().lower()\n", + " if choice == \"y\":\n", + " bonus -= 3\n", + " items.remove(\"holy_water\")\n", + " print(\"You splash holy water! The ghost recoils...\")\n", + "\n", + " if \"silver_dagger\" in items:\n", + " choice = input(\"Use silver_dagger for an advantage? (y/n): \").strip().lower()\n", + " if choice == \"y\":\n", + " bonus -= 2\n", + " items.remove(\"silver_dagger\")\n", + " print(\"You grip the silver dagger tightly...\")\n", + "\n", + " roll = random.randint(1, 10)\n", + " final_roll = max(1, min(10, roll + bonus)) # keep it in [1, 10]\n", + "\n", + " print(f\"(Ghost encounter roll: {roll} -> {final_roll})\")\n", + "\n", + " if final_roll <= 5:\n", + " print(\"You defeated the ghost!\")\n", + " return True\n", + " else:\n", + " print(\"You lost the battle...\")\n", + " return False" ] }, { @@ -101,8 +151,23 @@ "execution_count": null, "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Haunted Mansion!\n", + "\n", + "--- New attempt through the mansion ---\n", + "\n", + "Intersection 1/3\n", + "Health: 10/10 | Items: none\n" + ] + } + ], "source": [ + "import random\n", + "\n", "# main function for the game\n", "def run_mansion():\n", " \n", @@ -130,7 +195,108 @@ "\n", " \"\"\"\n", " \n", - " # your code goes here" + "def run_mansion():\n", + " print(\"Welcome to the Haunted Mansion!\")\n", + "\n", + " health = 10\n", + " max_health = 10\n", + " items = []\n", + "\n", + " ROOMS_TO_FINAL_DOOR = 3 # after these intersections, you reach the locked door\n", + "\n", + " def maybe_find_random_item():\n", + " \"\"\"Random small chance to find an item after moving through a room.\"\"\"\n", + " # Use a loop to possibly generate multiple minor events\n", + " for _ in range(random.randint(0, 1)):\n", + " if random.random() < 0.35:\n", + " found = random.choice(ITEM_POOL)\n", + " items.append(found)\n", + " print(f\"You found a {found} on a dusty shelf.\")\n", + "\n", + " def try_heal():\n", + " \"\"\"Let the player use healing items if available.\"\"\"\n", + " nonlocal health\n", + " if health >= max_health:\n", + " return\n", + "\n", + " # Prefer potion, then bandage\n", + " if \"potion\" in items:\n", + " choice = input(\"Use a potion to heal +3? (y/n): \").strip().lower()\n", + " if choice == \"y\":\n", + " items.remove(\"potion\")\n", + " health = min(max_health, health + 3)\n", + " print(f\"You feel better. Health is now {health}/{max_health}.\")\n", + "\n", + " elif \"bandage\" in items:\n", + " choice = input(\"Use a bandage to heal +2? (y/n): \").strip().lower()\n", + " if choice == \"y\":\n", + " items.remove(\"bandage\")\n", + " health = min(max_health, health + 2)\n", + " print(f\"You patch yourself up. Health is now {health}/{max_health}.\")\n", + "\n", + " # Keep playing until treasure found or health is gone\n", + " while health > 0:\n", + " print(\"\\n--- New attempt through the mansion ---\")\n", + "\n", + " # Walk through a sequence of intersections (for-loop),\n", + " # with input validation (nested while-loop)\n", + " for room in range(1, ROOMS_TO_FINAL_DOOR + 1):\n", + " if health <= 0:\n", + " break\n", + "\n", + " print(f\"\\nIntersection {room}/{ROOMS_TO_FINAL_DOOR}\")\n", + " print(f\"Health: {health}/{max_health} | Items: {items if items else 'none'}\")\n", + "\n", + " choice = input('Choose a path (\"left\" or \"right\"): ').strip().lower()\n", + " while choice not in (\"left\", \"right\"):\n", + " choice = input('Invalid choice. Type \"left\" or \"right\": ').strip().lower()\n", + "\n", + " if choice == \"left\":\n", + " # 50% potion, 50% trap (-2 health)\n", + " if random.random() < 0.5:\n", + " items.append(\"potion\")\n", + " print(\"You found a potion!\")\n", + " else:\n", + " health -= 2\n", + " print(\"A hidden trap snaps shut! You lose 2 health points.\")\n", + "\n", + " else: # choice == \"right\"\n", + " won = encounter_ghost(items)\n", + "\n", + " if won:\n", + " if \"key\" not in items:\n", + " items.append(\"key\")\n", + " print(\"The ghost drops a key. You take it.\")\n", + " else:\n", + " print(\"You already have a key. You move on carefully.\")\n", + " else:\n", + " health -= 2\n", + " print(\"You stagger back from the fight and lose 2 health points.\")\n", + "\n", + " maybe_find_random_item()\n", + " try_heal()\n", + "\n", + " if health <= 0:\n", + " break\n", + "\n", + " # After the intersections, you reach the final door\n", + " if health <= 0:\n", + " break\n", + "\n", + " print(\"\\nYou reach a heavy locked door at the end of the mansion...\")\n", + "\n", + " if \"key\" in items:\n", + " print(\"You unlocked the door and found the Treasure! Congratulations!\")\n", + " return\n", + " else:\n", + " print(\"The door is locked. You need to find the key. You return to the beginning...\")\n", + "\n", + " print(\"Game over, you lost all your health points.\")\n", + "\n", + "\n", + "# If you want the game to start immediately when you run the file:\n", + "if __name__ == \"__main__\":\n", + " run_mansion()" ] }, { @@ -146,7 +312,69 @@ "execution_count": null, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Haunted Mansion!\n", + "\n", + "--- New attempt through the mansion ---\n", + "\n", + "Intersection 1/3\n", + "Health: 10/10 | Items: none\n", + "You found a potion!\n", + "\n", + "Intersection 2/3\n", + "Health: 10/10 | Items: ['potion']\n", + "\n", + "You encounter a ghost!\n", + "(Ghost encounter roll: 9 -> 9)\n", + "You lost the battle...\n", + "You stagger back from the fight and lose 2 health points.\n", + "You feel better. Health is now 10/10.\n", + "\n", + "Intersection 3/3\n", + "Health: 10/10 | Items: none\n", + "You found a potion!\n", + "\n", + "You reach a heavy locked door at the end of the mansion...\n", + "The door is locked. You need to find the key. You return to the beginning...\n", + "\n", + "--- New attempt through the mansion ---\n", + "\n", + "Intersection 1/3\n", + "Health: 10/10 | Items: ['potion']\n", + "You found a potion!\n", + "\n", + "Intersection 2/3\n", + "Health: 10/10 | Items: ['potion', 'potion']\n", + "You found a potion!\n", + "You found a holy_water on a dusty shelf.\n", + "\n", + "Intersection 3/3\n", + "Health: 10/10 | Items: ['potion', 'potion', 'potion', 'holy_water']\n", + "You found a potion!\n", + "You found a potion on a dusty shelf.\n", + "\n", + "You reach a heavy locked door at the end of the mansion...\n", + "The door is locked. You need to find the key. You return to the beginning...\n", + "\n", + "--- New attempt through the mansion ---\n", + "\n", + "Intersection 1/3\n", + "Health: 10/10 | Items: ['potion', 'potion', 'potion', 'holy_water', 'potion', 'potion']\n", + "\n", + "You encounter a ghost!\n", + "(Ghost encounter roll: 4 -> 4)\n", + "You defeated the ghost!\n", + "The ghost drops a key. You take it.\n", + "\n", + "Intersection 2/3\n", + "Health: 10/10 | Items: ['potion', 'potion', 'potion', 'holy_water', 'potion', 'potion', 'key']\n" + ] + } + ], "source": [ "run_mansion()" ] @@ -162,7 +390,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -176,7 +404,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.11.0" } }, "nbformat": 4,