From 62d0a40e7f03f9e6526ca4f69e0d8e7d71c74cd7 Mon Sep 17 00:00:00 2001 From: Ekaterina Levchenko Date: Mon, 16 Feb 2026 17:57:58 +0100 Subject: [PATCH] Solution --- lab-python-flow-control.ipynb | 83 +++++++++++++++++++++++++++++------ 1 file changed, 70 insertions(+), 13 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..acafce9 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -77,7 +77,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, + "id": "a7ab83fd", + "metadata": {}, + "outputs": [], + "source": [ + "import random" + ] + }, + { + "cell_type": "code", + "execution_count": 19, "id": "499552c8-9e30-46e1-a706-4ac5dc64670e", "metadata": {}, "outputs": [], @@ -92,22 +102,26 @@ " and return something that indicates the ghost defeated the adventurer.\n", " \"\"\"\n", " print(\"You encounter a ghost!\")\n", - " \n", - " # your code goes here" + "\n", + " result = random.randrange(0, 10)\n", + " if result <= 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": 20, "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, "outputs": [], "source": [ + "\n", "# 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", @@ -129,8 +143,39 @@ " If they don't have the key, they are prompted to find it from the beginning.\n", "\n", " \"\"\"\n", - " \n", - " # your code goes here" + " \n", + " print(\"Welcome to the Haunted Mansion!\")\n", + " health = 10\n", + " items = []\n", + "\n", + " while health > 0 or \"key\" in items:\n", + " print(f\"Your health {health}\")\n", + " direction = input(\"Choose left or right path: \").lower()\n", + "\n", + " if direction == \"left\":\n", + " if random.choice([0,1]): # 50/50 chance that found a potion\n", + " items.append(\"potion\")\n", + " else: # IT IS A TRAP!\n", + " health -= 2\n", + " elif direction == \"right\":\n", + " if encounter_ghost():\n", + " items.append(\"key\")\n", + " else:\n", + " health -= 2\n", + " else:\n", + " print(\"Wrong input. Write left or write right\")\n", + "\n", + " if health == 0:\n", + " print(\"Game over, you lost all your health points.\")\n", + " return\n", + " \n", + " if \"key\" in items:\n", + " print(\"You unlocked the door and found the Treasure! Congratulations!\")\n", + " return\n", + "\n", + " print(\"No key found yet! Go on!\")\n", + " \n", + " " ] }, { @@ -143,10 +188,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Haunted Mansion!\n", + "Your health 10\n", + "You encounter a ghost!\n", + "You defeated the ghost!\n", + "You unlocked the door and found the Treasure! Congratulations!\n" + ] + } + ], "source": [ "run_mansion()" ] @@ -162,7 +219,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "ironhack_prework", "language": "python", "name": "python3" }, @@ -176,7 +233,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.11.14" } }, "nbformat": 4,