From 6121d3da1d0ac149a311d64cfe9f10fdf1c7a054 Mon Sep 17 00:00:00 2001 From: bperezlovisolo Date: Tue, 13 Jan 2026 23:33:12 +0100 Subject: [PATCH 1/2] lab-python-flow-extra --- lab-python-flow-control.ipynb | 84 +++++++++++++++++++++++++++++++---- 1 file changed, 76 insertions(+), 8 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..c935e6d 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": 16, "id": "499552c8-9e30-46e1-a706-4ac5dc64670e", "metadata": {}, "outputs": [], @@ -93,12 +93,20 @@ " \"\"\"\n", " print(\"You encounter a ghost!\")\n", " \n", - " # your code goes here" + " # your code goes here\n", + "\n", + " number = int(input(\"Choose a number between 1 and 10: \"))\n", + " if number <= 5:\n", + " print(\"You defeated the ghost!\")\n", + " return True\n", + " else:\n", + " print(\"You lost the battle...\")\n", + " return False" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, "outputs": [], @@ -130,7 +138,46 @@ "\n", " \"\"\"\n", " \n", - " # your code goes here" + " # your code goes here\n", + "\n", + " health = 10\n", + " items = []\n", + " \n", + " while health > 0:\n", + " print(\"Health:\", health)\n", + " print(\"Items:\", items)\n", + " \n", + " choice = input(\"Choose a path (left/right): \").lower()\n", + " \n", + " if choice == \"left\":\n", + " event = input(\"Type 'potion' or 'trap': \").lower()\n", + " \n", + " if event == \"potion\":\n", + " print(\"You found a potion!\")\n", + " items.append(\"potion\")\n", + " elif event == \"trap\":\n", + " print(\"You fell into a trap!\")\n", + " health -= 2\n", + " \n", + " elif choice == \"right\":\n", + " won = encounter_ghost()\n", + " \n", + " if won:\n", + " print(\"You found a key!\")\n", + " items.append(\"key\")\n", + " else:\n", + " health -= 2\n", + " \n", + " else:\n", + " print(\"Invalid choice, try again.\")\n", + " \n", + " if \"key\" in items:\n", + " print(\"You unlocked the door and found the Treasure! Congratulations!\")\n", + " break\n", + " \n", + " if health <= 0:\n", + " print(\"Game over, you lost all your health points.\")\n", + " \n" ] }, { @@ -143,10 +190,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Haunted Mansion!\n", + "Health: 10\n", + "Items: []\n", + "You fell into a trap!\n", + "Health: 8\n", + "Items: []\n", + "You encounter a ghost!\n", + "You lost the battle...\n", + "Health: 6\n", + "Items: []\n", + "You encounter a ghost!\n", + "You defeated the ghost!\n", + "You found a key!\n", + "You unlocked the door and found the Treasure! Congratulations!\n" + ] + } + ], "source": [ "run_mansion()" ] @@ -162,7 +230,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -176,7 +244,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.2" } }, "nbformat": 4, From c12868230152f8eccb7148639261df0450d6a75d Mon Sep 17 00:00:00 2001 From: bperezlovisolo Date: Tue, 13 Jan 2026 23:46:46 +0100 Subject: [PATCH 2/2] lab-python-flow-extra --- lab-python-flow-control.ipynb | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index c935e6d..c098993 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -77,11 +77,13 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 26, "id": "499552c8-9e30-46e1-a706-4ac5dc64670e", "metadata": {}, "outputs": [], "source": [ + "\n", + "import random\n", "def encounter_ghost():\n", " \"\"\"\n", " This function handles the encounter with a ghost. \n", @@ -95,7 +97,8 @@ " \n", " # your code goes here\n", "\n", - " number = int(input(\"Choose a number between 1 and 10: \"))\n", + " number = random.randint(1, 10)\n", + " \n", " if number <= 5:\n", " print(\"You defeated the ghost!\")\n", " return True\n", @@ -106,12 +109,13 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 27, "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, "outputs": [], "source": [ "# main function for the game\n", + "import random\n", "def run_mansion():\n", " \n", " print(\"Welcome to the Haunted Mansion!\")\n", @@ -150,7 +154,7 @@ " choice = input(\"Choose a path (left/right): \").lower()\n", " \n", " if choice == \"left\":\n", - " event = input(\"Type 'potion' or 'trap': \").lower()\n", + " event = random.choice([\"potion\", \"trap\"])\n", " \n", " if event == \"potion\":\n", " print(\"You found a potion!\")\n", @@ -190,7 +194,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 29, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, "outputs": [ @@ -201,12 +205,9 @@ "Welcome to the Haunted Mansion!\n", "Health: 10\n", "Items: []\n", - "You fell into a trap!\n", - "Health: 8\n", - "Items: []\n", "You encounter a ghost!\n", "You lost the battle...\n", - "Health: 6\n", + "Health: 8\n", "Items: []\n", "You encounter a ghost!\n", "You defeated the ghost!\n",