From becf6efb76a03e1857523c2cf8c36b22dc4d162d Mon Sep 17 00:00:00 2001 From: Juan Boberg Aguirre Date: Fri, 24 Apr 2026 12:03:20 +0200 Subject: [PATCH 1/2] Update lab-python-flow-control.ipynb --- lab-python-flow-control.ipynb | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..99c333f 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": 3, "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": [], @@ -143,10 +143,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'run_mansion' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[1]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m run_mansion()\n", + "\u001b[31mNameError\u001b[39m: name 'run_mansion' is not defined" + ] + } + ], "source": [ "run_mansion()" ] @@ -162,7 +174,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -176,7 +188,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.4" } }, "nbformat": 4, From 2aa972ecd90be438b9bc4cf2b26547b32b923606 Mon Sep 17 00:00:00 2001 From: Juan Boberg Aguirre Date: Thu, 11 Jun 2026 16:23:33 +0200 Subject: [PATCH 2/2] Solved lab --- ...=> lab-python-flow-control-extra (2).ipynb | 72 +++---------------- 1 file changed, 8 insertions(+), 64 deletions(-) rename lab-python-flow-control.ipynb => lab-python-flow-control-extra (2).ipynb (63%) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control-extra (2).ipynb similarity index 63% rename from lab-python-flow-control.ipynb rename to lab-python-flow-control-extra (2).ipynb index 99c333f..69a6db6 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control-extra (2).ipynb @@ -77,61 +77,19 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "499552c8-9e30-46e1-a706-4ac5dc64670e", "metadata": {}, "outputs": [], - "source": [ - "def encounter_ghost():\n", - " \"\"\"\n", - " This function handles the encounter with a ghost. \n", - " The outcome of the battle is determined by a random number between 1 and 10.\n", - " If the random number is less than or equal to 5, the adventurer defeats the ghost. In this case, print the message \"You defeated the ghost!\" \n", - " and return something that indicates the adventurer defeated the ghost.\n", - " 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", - " print(\"You encounter a ghost!\")\n", - " \n", - " # your code goes here" - ] + "source": "def encounter_ghost():\n \"\"\"\n This function handles the encounter with a ghost. \n The outcome of the battle is determined by a random number between 1 and 10.\n If the random number is less than or equal to 5, the adventurer defeats the ghost. In this case, print the message \"You defeated the ghost!\" \n and return something that indicates the adventurer defeated the ghost.\n 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 import random\n print(\"You encounter a ghost!\")\n \n roll = random.randint(1, 10)\n if roll <= 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": 2, + "execution_count": null, "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, "outputs": [], - "source": [ - "# main function for the game\n", - "def run_mansion():\n", - " \n", - " print(\"Welcome to the Haunted Mansion!\")\n", - " \n", - " \"\"\"\n", - " Simulates an adventure through a haunted mansion. The adventurer starts with 10 health points and no items.\n", - " Prompt the user to choose between two paths: \"left\" or \"right\". \n", - "\n", - " If they choose \"left\", a random event occurs. There is a 50% chance that the adventurer will find a potion and a 50% chance that they will \n", - " fall into a trap and lose 2 health points. If they find the potion, it is saved into the adventurer's items. \n", - " If they fall into a trap, 2 points are taken out of the adventurer's health points.\n", - "\n", - " If they choose \"right\", the \"encounter_ghost()\" function is called to handle the battle between the adventurer and the ghost. \n", - " If the adventurer wins, they find a key which is saved into the adventurer's items. If they lose, they lose 2 health points.\n", - " Hint: remember to check what encounter_ghost() returned to make know if they won or lost.\n", - "\n", - " If the adventurer chooses something other than \"left\" or \"right\", they are prompted to try again.\n", - "\n", - " If the adventurer's health points reach 0 or less, the message \"Game over, you lost all your health points.\" is printed.\n", - "\n", - " If the adventurer has the key, they can unlock the door and find the Treasure. This message is printed \"You unlocked the door and found the \n", - " Treasure! Congratulations!\".\n", - " If they don't have the key, they are prompted to find it from the beginning.\n", - "\n", - " \"\"\"\n", - " \n", - " # your code goes here" - ] + "source": "# main function for the game\ndef run_mansion():\n import random\n\n print(\"Welcome to the Haunted Mansion!\")\n\n health_points = 10\n items = []\n\n while health_points > 0:\n # Prompt user for path choice\n choice = input(\"\\nChoose a path: 'left' or 'right': \").strip().lower()\n\n if choice == \"left\":\n # 50% chance: find potion or fall into trap\n event = random.randint(1, 2)\n if event == 1:\n print(\"You found a potion! +2 health restored.\")\n items.append(\"potion\")\n health_points += 2\n else:\n print(\"You fell into a trap! You lose 2 health points.\")\n health_points -= 2\n\n elif choice == \"right\":\n # Encounter a ghost\n won = encounter_ghost()\n if won:\n print(\"You found a key!\")\n items.append(\"key\")\n else:\n print(\"You lose 2 health points.\")\n health_points -= 2\n\n else:\n print(\"Invalid choice. Please try again.\")\n continue\n\n print(f\"Health: {health_points} | Items: {items}\")\n\n # Check if health is depleted\n if health_points <= 0:\n print(\"Game over, you lost all your health points.\")\n return\n\n # Check win condition\n if \"key\" in items:\n print(\"\\nYou unlocked the door and found the Treasure! Congratulations!\")\n return\n else:\n print(\"You need to find the key to unlock the treasure door. Keep exploring!\")" }, { "cell_type": "markdown", @@ -143,25 +101,11 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'run_mansion' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[31m---------------------------------------------------------------------------\u001b[39m", - "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", - "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[1]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m run_mansion()\n", - "\u001b[31mNameError\u001b[39m: name 'run_mansion' is not defined" - ] - } - ], - "source": [ - "run_mansion()" - ] + "outputs": [], + "source": "run_mansion()" }, { "cell_type": "markdown", @@ -193,4 +137,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} +} \ No newline at end of file