From 5d29f38c12337c55c0fa00971501a804c05948a9 Mon Sep 17 00:00:00 2001 From: martin-paz-y Date: Wed, 10 Sep 2025 21:14:48 +0200 Subject: [PATCH 1/2] Solved lab --- lab-python-flow-control.ipynb | 107 ++++++++++++++++++++++++++++++---- 1 file changed, 95 insertions(+), 12 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..d56427b 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -75,6 +75,20 @@ " Good luck!" ] }, + { + "cell_type": "code", + "execution_count": 1, + "id": "7213c6be", + "metadata": {}, + "outputs": [], + "source": [ + "inventory=[\"knife\",\"flower\",\"shield\", \"axe\"]\n", + "\n", + "INIT_GAME_STATE = {\n", + " \"health\": 10,\n", + "}" + ] + }, { "cell_type": "code", "execution_count": null, @@ -93,23 +107,73 @@ " \"\"\"\n", " print(\"You encounter a ghost!\")\n", " \n", - " # your code goes here" + " # your code goes here\n", + "\n", + " def encounter_ghost():\n", + " print(\"A ghost appeared! Buuuuuu\")\n", + " action = input(\"What would you like to do, run or fight?: \").strip().lower()\n", + " if action == \"fight\":\n", + " if \"axe\" in inventory:\n", + " if inventory == \"axe\":\n", + " print(\"You killed him, You Win\")\n", + " else:\n", + " print(\"You die\")\n", + " elif action == \"run\":\n", + " print(\"You run away as fast as you can! 🏃‍♂️ The ghost disappears...\")\n", + " run_mansion()\n", + "\n", + " else:\n", + " print(\"You hesitate... and the ghost takes advantage. -8 damage.\")\n", + " INIT_GAME_STATE[\"health\"] -= 8\n", + " if INIT_GAME_STATE[\"health\"] < 1:\n", + " print(\"You die\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'\\nSimulates 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'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "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", + " direction = input(\"What direction would you take? Type 'left' or 'right'?: \").strip().lower()\n", + "\n", + " if direction == \"right\":\n", + " encounter_ghost()\n", + " elif direction == \"left\":\n", + " # 50% también a la izquierda (de ejemplo)\n", + " if id(object()) % 2 == 0:\n", + " print(\"You went left. It's very quiet adn you fall in a trap -2 HEALTH POINTS\")\n", + " INIT_GAME_STATE[\"health\"] -= 2\n", + " if INIT_GAME_STATE[\"health\"] <= 0:\n", + " print(\"You die\")\n", + " else:\n", + " print(f\"You survive the trap. Health is now {INIT_GAME_STATE['health']}.\")\n", + " run_mansion()\n", + " else:\n", + " print(\"You went left. It's very quiet...You found a potion\")\n", + " inventory.__add__(potion)\n", + " else:\n", + " print(\"Wrong value try again\")\n", + " run_mansion()\n", + "\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", @@ -128,7 +192,7 @@ " Treasure! Congratulations!\".\n", " If they don't have the key, they are prompted to find it from the beginning.\n", "\n", - " \"\"\"\n", + "\"\"\"\n", " \n", " # your code goes here" ] @@ -143,10 +207,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Haunted Mansion!\n", + "Wrong value try again\n", + "Welcome to the Haunted Mansion!\n", + "You went left. It's very quiet adn you fall in a trap -2 HEALTH POINTS\n", + "You survive the trap. Health is now 8.\n", + "Welcome to the Haunted Mansion!\n", + "You went left. It's very quiet adn you fall in a trap -2 HEALTH POINTS\n", + "You survive the trap. Health is now 6.\n", + "Welcome to the Haunted Mansion!\n", + "A ghost appeared! Buuuuuu\n", + "You hesitate... and the ghost takes advantage. -8 damage.\n", + "You die\n" + ] + } + ], "source": [ "run_mansion()" ] @@ -162,7 +245,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -176,7 +259,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.7" } }, "nbformat": 4, From cfd25880e9b64d44a76c6f36b8192d3954664e5c Mon Sep 17 00:00:00 2001 From: martin-paz-y Date: Thu, 11 Sep 2025 09:01:13 +0200 Subject: [PATCH 2/2] Solved Lab2 --- lab-python-flow-control.ipynb | 100 +++++++++++++++++++++++----------- 1 file changed, 69 insertions(+), 31 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index d56427b..b3435ba 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -77,7 +77,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "7213c6be", "metadata": {}, "outputs": [], @@ -86,6 +86,7 @@ "\n", "INIT_GAME_STATE = {\n", " \"health\": 10,\n", + " \"trasure_key\": False\n", "}" ] }, @@ -109,29 +110,56 @@ " \n", " # your code goes here\n", "\n", - " def encounter_ghost():\n", - " print(\"A ghost appeared! Buuuuuu\")\n", + "import random # cuidado, es random, no \"ramdom\"\n", + "\n", + "def run_mansion():\n", + " print(\"🏰 You are back in the mansion...\")\n", + "\n", + "def encounter_ghost():\n", + " print(\"A ghost appeared! 👻 Buuuuuu\")\n", " action = input(\"What would you like to do, run or fight?: \").strip().lower()\n", + "\n", " if action == \"fight\":\n", " if \"axe\" in inventory:\n", - " if inventory == \"axe\":\n", - " print(\"You killed him, You Win\")\n", + " # 80% de probabilidad de ganar\n", + " if random.random() < 0.8:\n", + " print(\"⚔️ You swing your axe and kill the ghost.\")\n", + " print(\"You foun a Key, Lets unlock the treasure\")\n", + " INIT_GAME_STATE[\"treasure_key\"] = True\n", + " def treasure():\n", + " \n", + "\n", " else:\n", - " print(\"You die\")\n", + " print(\"You hesitate... and the ghost takes advantage. -8 damage.\")\n", + " INIT_GAME_STATE[\"health\"] -= 8\n", + " if INIT_GAME_STATE[\"health\"] < 1:\n", + " print(\"☠️ You die.\")\n", + " else:\n", + " run_mansion()\n", + " else:\n", + " print(\"You try to fight with your bare hands... -8 damage.\")\n", + " INIT_GAME_STATE[\"health\"] -= 8\n", + " if INIT_GAME_STATE[\"health\"] < 1:\n", + " print(\"☠️ You die.\")\n", + " else:\n", + " run_mansion()\n", + "\n", " elif action == \"run\":\n", - " print(\"You run away as fast as you can! 🏃‍♂️ The ghost disappears...\")\n", + " print(\"🏃 You run away as fast as you can! The ghost disappears...\")\n", " run_mansion()\n", "\n", " else:\n", " print(\"You hesitate... and the ghost takes advantage. -8 damage.\")\n", " INIT_GAME_STATE[\"health\"] -= 8\n", " if INIT_GAME_STATE[\"health\"] < 1:\n", - " print(\"You die\")" + " print(\"☠️ You die.\")\n", + " else:\n", + " run_mansion()\n" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, "outputs": [ @@ -148,30 +176,34 @@ ], "source": [ "# main function for the game\n", + "import random\n", + "\n", "def run_mansion():\n", " print(\"Welcome to the Haunted Mansion!\")\n", " direction = input(\"What direction would you take? Type 'left' or 'right'?: \").strip().lower()\n", "\n", " if direction == \"right\":\n", - " encounter_ghost()\n", + " encounter_ghost() # Asumo que ya la tienes definida\n", + "\n", " elif direction == \"left\":\n", - " # 50% también a la izquierda (de ejemplo)\n", - " if id(object()) % 2 == 0:\n", - " print(\"You went left. It's very quiet adn you fall in a trap -2 HEALTH POINTS\")\n", + " # 50% de caer en trampa vs encontrar poción\n", + " if random.random() < 0.5: # ← 50/50 real\n", + " print(\"You went left. It's very quiet and you fall into a trap. -2 HEALTH POINTS\")\n", " INIT_GAME_STATE[\"health\"] -= 2\n", " if INIT_GAME_STATE[\"health\"] <= 0:\n", - " print(\"You die\")\n", + " print(\"You die\")\n", " else:\n", " print(f\"You survive the trap. Health is now {INIT_GAME_STATE['health']}.\")\n", " run_mansion()\n", " else:\n", - " print(\"You went left. It's very quiet...You found a potion\")\n", - " inventory.__add__(potion)\n", + " print(\"You went left. It's very quiet... You found a potion!\")\n", + " inventory.append(\"potion\") # ← en lugar de inventory.__add__(potion)\n", + "\n", " else:\n", - " print(\"Wrong value try again\")\n", + " print(\"Wrong value, try again.\")\n", " run_mansion()\n", "\n", - " \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", @@ -197,6 +229,23 @@ " # your code goes here" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "4b36ca28", + "metadata": {}, + "outputs": [], + "source": [ + "def treasure():\n", + " if INIT_GAME_STATE[\"treasure_key\"] = True\n", + " print \"You got all the gold\"\n", + " else \n", + " print \"You need the key to unlock the treaure\"\n", + " def run_mansion():\n", + " \n", + " " + ] + }, { "cell_type": "markdown", "id": "9e13a33c-38e5-44b3-bd1b-9a642c962c89", @@ -207,7 +256,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 12, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, "outputs": [ @@ -215,18 +264,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Welcome to the Haunted Mansion!\n", - "Wrong value try again\n", - "Welcome to the Haunted Mansion!\n", - "You went left. It's very quiet adn you fall in a trap -2 HEALTH POINTS\n", - "You survive the trap. Health is now 8.\n", - "Welcome to the Haunted Mansion!\n", - "You went left. It's very quiet adn you fall in a trap -2 HEALTH POINTS\n", - "You survive the trap. Health is now 6.\n", - "Welcome to the Haunted Mansion!\n", - "A ghost appeared! Buuuuuu\n", - "You hesitate... and the ghost takes advantage. -8 damage.\n", - "You die\n" + "🏰 You are back in the mansion...\n" ] } ],