From abf2c78bfc2af9c3b413dca98630645942091a6d Mon Sep 17 00:00:00 2001 From: Ricardo Santos Date: Fri, 16 Jan 2026 14:03:29 +0000 Subject: [PATCH 1/2] lab done: flow control extra --- lab-python-flow-control.ipynb | 115 ++++++++++++++++++++++++++++------ 1 file changed, 97 insertions(+), 18 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..64a51ce 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -77,11 +77,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "id": "499552c8-9e30-46e1-a706-4ac5dc64670e", "metadata": {}, "outputs": [], "source": [ + "list_items = [\"bow\", \"shield\", \"armor\", \"spear\", \"helmet\", \"sword\"]\n", + "\n", + "import random\n", + "\n", + "\n", "def encounter_ghost():\n", " \"\"\"\n", " This function handles the encounter with a ghost. \n", @@ -91,20 +96,22 @@ " If the random number is greater than 5, the adventurer loses the battle. In this case, print \"You lost the battle...\"\n", " and return something that indicates the ghost defeated the adventurer.\n", " \"\"\"\n", + "\n", " print(\"You encounter a ghost!\")\n", + " r = random.randint(1, 10)\n", " \n", - " # your code goes here" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", - "metadata": {}, - "outputs": [], - "source": [ + "\n", + " if r <= 5:\n", + " print(\"You defeated the ghost!\")\n", + " result = \"Won the battle\"\n", + " else:\n", + " print(\"You lost the battle...\")\n", + " result = \"Lost the battle\"\n", + " return result\n", + " \n", + " # your code goes here\n", "# main function for the game\n", - "def run_mansion():\n", + "def run_mansion(result):\n", " \n", " print(\"Welcome to the Haunted Mansion!\")\n", " \n", @@ -129,7 +136,49 @@ " If they don't have the key, they are prompted to find it from the beginning.\n", "\n", " \"\"\"\n", - " \n", + " items = []\n", + " health_points = 10\n", + "\n", + " while health_points > 0:\n", + " while True:\n", + " try:\n", + " p = input(\"Choose a path (left/right): \").lower()\n", + " if p != \"left\" or p != \"right\":\n", + " print(\"Your choice has to be: left or right\")\n", + " break\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter 'left' or 'right'.\")\n", + " if p == \"left\":\n", + " r1 = random.randint(1,2)\n", + " if r1 == 1:\n", + " print(\"You fell into a trap!! Lost 2 HP\")\n", + " health_points -= 2\n", + " elif r1 == 2:\n", + " items.append(\"potion\")\n", + " print(\"You found a potion!\")\n", + " if p == \"right\":\n", + " result = encounter_ghost()\n", + " if result == \"Won the battle\":\n", + " print(\"You found a key!\")\n", + " items.append(\"key\")\n", + " elif result == \"Lost the battle\":\n", + " print(\"Efect of the battle: Lost 2 HP\")\n", + " health_points -= 2\n", + " if \"key\" in items:\n", + " print(\"Now, you found a door to use that key!\")\n", + " print(\"You unlocked the door and found the Treasure! Congratulations!\")\n", + " for item in list_items:\n", + " print(f\"You also found a {item} inside the treasure!\")\n", + " break\n", + " if health_points <= 0:\n", + " print(\"Game over, you lost all your health points.\")\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", " # your code goes here" ] }, @@ -143,12 +192,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Haunted Mansion!\n", + "Your choice has to be: left or right\n", + "You encounter a ghost!\n", + "You lost the battle...\n", + "Efect of the battle: Lost 2 HP\n", + "Your choice has to be: left or right\n", + "You encounter a ghost!\n", + "You lost the battle...\n", + "Efect of the battle: Lost 2 HP\n", + "Your choice has to be: left or right\n", + "You encounter a ghost!\n", + "You defeated the ghost!\n", + "You found a key!\n", + "Now, you found a door to use that key!\n", + "You unlocked the door and found the Treasure! Congratulations!\n", + "You also found a bow inside the treasure!\n", + "You also found a shield inside the treasure!\n", + "You also found a armor inside the treasure!\n", + "You also found a spear inside the treasure!\n", + "You also found a helmet inside the treasure!\n", + "You also found a sword inside the treasure!\n", + "Game over, you lost all your health points.\n" + ] + } + ], "source": [ - "run_mansion()" + "\n", + "run_mansion(result)" ] }, { @@ -162,7 +241,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -176,7 +255,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4, From e5c060f1c3dcfecf2c833b12f42fb6f6b7ac85e9 Mon Sep 17 00:00:00 2001 From: Ricardo Santos Date: Fri, 16 Jan 2026 14:07:25 +0000 Subject: [PATCH 2/2] lab done: flow control extra2 --- lab-python-flow-control.ipynb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 64a51ce..ee2552c 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -77,7 +77,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 1, "id": "499552c8-9e30-46e1-a706-4ac5dc64670e", "metadata": {}, "outputs": [], @@ -111,7 +111,7 @@ " \n", " # your code goes here\n", "# main function for the game\n", - "def run_mansion(result):\n", + "def run_mansion():\n", " \n", " print(\"Welcome to the Haunted Mansion!\")\n", " \n", @@ -143,7 +143,7 @@ " while True:\n", " try:\n", " p = input(\"Choose a path (left/right): \").lower()\n", - " if p != \"left\" or p != \"right\":\n", + " if p != \"left\" and p != \"right\":\n", " print(\"Your choice has to be: left or right\")\n", " break\n", " except ValueError:\n", @@ -192,7 +192,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, "outputs": [ @@ -227,7 +227,7 @@ ], "source": [ "\n", - "run_mansion(result)" + "run_mansion()" ] }, {