From 8a0dea93302cc9628006052bc7c09b38ae920710 Mon Sep 17 00:00:00 2001 From: Feliciu Svabu Date: Fri, 20 Feb 2026 15:01:34 +0000 Subject: [PATCH] Solved lab --- lab-python-flow-control.ipynb | 196 ++++++++++++++++++++++++++++------ 1 file changed, 163 insertions(+), 33 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..7e31375 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -75,6 +75,86 @@ " Good luck!" ] }, + { + "cell_type": "code", + "execution_count": 18, + "id": "84f77936", + "metadata": {}, + "outputs": [], + "source": [ + "import random\n", + "\n", + "# Inventory (Cross, Shield, Holy Water, Salt)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "b0c9aab9", + "metadata": {}, + "outputs": [], + "source": [ + "def ghosty(inventory, health):\n", + " print(\"šŸŒ‘ A Ghosty emerges! It drains your energy slowly.\")\n", + " damage = random.randint(1, 3)\n", + "\n", + " if \"Cross\" in inventory:\n", + " print(f\"You fight back with your cross! The Ghosty vanished\")\n", + " else:\n", + " return health - damage" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "e67d6e0f", + "metadata": {}, + "outputs": [], + "source": [ + "def poltergeist(inventory, health):\n", + " print(\"šŸŖ‘ A Poltergeist threw a chair on you.\")\n", + " damage = random.randint(1, 4)\n", + "\n", + " if \"Shield\" in inventory:\n", + " print(f\"You protected yourself with the shield! The Poltergeist vanished\")\n", + " else:\n", + " return health - damage" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "ea7f4393", + "metadata": {}, + "outputs": [], + "source": [ + "def banshee(inventory, health):\n", + " print(\"šŸ“¢ A Banshee appeared\")\n", + " damage = random.randint(2, 4)\n", + "\n", + " if \"Holy Water\" in inventory:\n", + " print(f\"You threw the Holy Water on Banshee! The Banshee vanished\")\n", + " else:\n", + " return health - damage" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "f7ed4a5e", + "metadata": {}, + "outputs": [], + "source": [ + "def spook(inventory, health):\n", + " print(\"šŸ‘» A Spooky appeared\")\n", + " damage = round(health * random.uniform(0, 0.5), 2)\n", + "\n", + " if \"Salt\" in inventory:\n", + " print(f\"You threw the Salt on Spooky! The Spooky vanished\")\n", + " else:\n", + " return health - damage" + ] + }, { "cell_type": "code", "execution_count": null, @@ -82,18 +162,33 @@ "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", + "def encounter_ghost(inventory, health):\n", " print(\"You encounter a ghost!\")\n", - " \n", - " # your code goes here" + " # Create the ghosts list\n", + " ghost_types = [\"Banshee\", \"Ghosty\", \"Poltergeist\", \"Spooky\"]\n", + " # pick a random ghost\n", + " pick_ghost = random.choice(ghost_types)\n", + "\n", + " # Create the list of items in the bad\n", + " item = [\"Cross\", \"Shield\", \"Holy Water\", \"Salt\"]\n", + " # pick a random item\n", + " pick_item = random.choice(item)\n", + " inventory.append(pick_item)\n", + "\n", + " if pick_ghost == \"Banshee\":\n", + " health = banshee(inventory, health)\n", + " print(f\"you found the item {pick_item}\")\n", + " elif pick_ghost == \"Poltergeist\":\n", + " health = poltergeist(inventory, health)\n", + " print(f\"you found the item {pick_item}\")\n", + " elif pick_ghost == \"Ghosty\":\n", + " health = ghosty(inventory, health)\n", + " print(f\"you found the item {pick_item}\")\n", + " elif pick_ghost == \"Spooky\":\n", + " health = spook(inventory, health)\n", + " print(f\"you found the item {pick_item}\")\n", + "\n", + " return health" ] }, { @@ -105,31 +200,49 @@ "source": [ "# main function for the game\n", "def run_mansion():\n", - " \n", + "\n", + " health = 10\n", + " inventory = []\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", + " while health > 0:\n", + " # Check if the key is in the inventory\n", + " if \"Key\" in inventory:\n", + " print(\"\\nšŸ”‘ You use the Key to unlock the heavy iron door...\")\n", + " print(\"šŸ’° You found the Treasure! Congratulations!\")\n", + " return # End the game successfully\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", + " print(f\"\\nHP: {health}\")\n", + " choice = input(\"Choose your path: 'left' or 'right': \").lower().strip()\n", "\n", - " If the adventurer chooses something other than \"left\" or \"right\", they are prompted to try again.\n", + " if choice == \"left\":\n", + " # 50/50 Chance logic\n", + " if random.random() < 0.5:\n", + " print(\"Empty room\")\n", + " else:\n", + " health = encounter_ghost(inventory, health)\n", + " if health <= 0:\n", + " print(\"You loose\")\n", + " else:\n", + " if random.random() < 0.5:\n", + " print(\"You found the key šŸ—ļø\")\n", + " inventory.append(\"Key\")\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", + " elif choice == \"right\":\n", + " if random.random() > 0.5:\n", + " print(\"Empty room\")\n", + " else:\n", + " health = encounter_ghost(inventory, health)\n", + " if health <= 0:\n", + " print(\"You loose\")\n", + " else:\n", + " if random.random() < 0.5:\n", + " print(\"You found the key šŸ—ļø\")\n", + " inventory.append(\"Key\")\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", + " else:\n", + " print(\"The shadows whhisper...choose left or rhight door\")\n", "\n", - " \"\"\"\n", - " \n", " # your code goes here" ] }, @@ -143,10 +256,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Haunted Mansion!\n", + "\n", + "HP: 10 | Items: []\n", + "You encounter a ghost!\n", + "šŸ‘» A Spooky appeared\n", + "you found the item ['Cross']\n", + "You found the key šŸ—ļø\n", + "\n", + "šŸ”‘ You use the Key to unlock the heavy iron door...\n", + "šŸ’° You found the Treasure! Congratulations!\n" + ] + } + ], "source": [ "run_mansion()" ] @@ -162,7 +292,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -176,7 +306,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.2" } }, "nbformat": 4,