From 0ce3fcc829ba22fbb8c652259033b70dc4a31692 Mon Sep 17 00:00:00 2001 From: martacarballo Date: Sat, 11 Oct 2025 23:22:20 +0200 Subject: [PATCH] solvedextraflow --- lab-python-flow-control.ipynb | 94 +++++++++++++++++++++++++++++++---- 1 file changed, 84 insertions(+), 10 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..12a76ce 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -77,11 +77,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "499552c8-9e30-46e1-a706-4ac5dc64670e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You lost the battle...\n", + "False\n" + ] + } + ], "source": [ + "import random\n", + "\n", "def encounter_ghost():\n", " \"\"\"\n", " This function handles the encounter with a ghost. \n", @@ -91,14 +102,21 @@ " 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" + " number = random.randint(1, 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\n", + "fin = encounter_ghost()\n", + "print(fin)\n", + " " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, "outputs": [], @@ -129,6 +147,39 @@ " If they don't have the key, they are prompted to find it from the beginning.\n", "\n", " \"\"\"\n", + " health_points = 10\n", + " items = []\n", + " while health_points > 0:\n", + " choice = input(\"Choose a path (left/right): \").strip().lower()\n", + " if choice == \"left\":\n", + " event = random.choice([\"potion\", \"trap\"])\n", + " if event == \"potion\":\n", + " print(\"You found a potion!\")\n", + " items.append(\"potion\")\n", + " else:\n", + " print(\"You fell into a trap and lost 2 health points.\")\n", + " health_points -= 2\n", + " elif choice == \"right\":\n", + " if encounter_ghost():\n", + " print(\"You found a key!\")\n", + " items.append(\"key\")\n", + " else:\n", + " print(\"You lost 2 health points.\")\n", + " health_points -= 2\n", + " else:\n", + " print(\"Invalid choice, please try again.\")\n", + " continue\n", + " \n", + " if health_points <= 0:\n", + " print(\"Game over, you lost all your health points.\")\n", + " break\n", + " \n", + " if \"key\" in items:\n", + " print(\"You unlocked the door and found the Treasure! Congratulations!\")\n", + " break\n", + " else:\n", + " print(\"You need to find the key to unlock the door.\")\n", + " print(f\"Health: {health_points} | Items: {items}\")\n", " \n", " # your code goes here" ] @@ -143,10 +194,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Haunted Mansion!\n", + "You fell into a trap and lost 2 health points.\n", + "You need to find the key to unlock the door.\n", + "Health: 8 | Items: []\n", + "You found a potion!\n", + "You need to find the key to unlock the door.\n", + "Health: 8 | Items: ['potion']\n", + "You fell into a trap and lost 2 health points.\n", + "You need to find the key to unlock the door.\n", + "Health: 6 | Items: ['potion']\n", + "You fell into a trap and lost 2 health points.\n", + "You need to find the key to unlock the door.\n", + "Health: 4 | Items: ['potion']\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 +236,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -176,7 +250,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.9" } }, "nbformat": 4,