From b32334ac0f2e262c3b730e1a9da6108871b90755 Mon Sep 17 00:00:00 2001 From: gabrieleb76-bot Date: Sat, 25 Apr 2026 10:14:13 +0100 Subject: [PATCH] Gabriel_lab-python-flow-control.ipynb --- lab-python-flow-control.ipynb | 126 +++++++++++++++++++++++++++++++--- 1 file changed, 118 insertions(+), 8 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..1929efe 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": 1, "id": "499552c8-9e30-46e1-a706-4ac5dc64670e", "metadata": {}, "outputs": [], @@ -93,12 +93,12 @@ " \"\"\"\n", " print(\"You encounter a ghost!\")\n", " \n", - " # your code goes here" + " import random" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, "outputs": [], @@ -130,7 +130,19 @@ "\n", " \"\"\"\n", " \n", - " # your code goes here" + " import random\n", + "\n", + "def encounter_ghost():\n", + " print(\"You encounter a ghost!\")\n", + "\n", + " random_number = random.randint(1, 10)\n", + "\n", + " if random_number <= 5:\n", + " print(\"You defeated the ghost!\")\n", + " return True\n", + " else:\n", + " print(\"You lost the battle...\")\n", + " return False" ] }, { @@ -143,11 +155,109 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", + "execution_count": 5, + "id": "71294f56", "metadata": {}, "outputs": [], "source": [ + "def run_mansion():\n", + " print(\"Welcome to the Haunted Mansion!\")\n", + "\n", + " health = 10\n", + " items = []\n", + "\n", + " while health > 0:\n", + " print(\"\\nYour health:\", health)\n", + " print(\"Your items:\", items)\n", + "\n", + " choice = input(\"Choose a path: left or right: \").lower()\n", + "\n", + " if choice == \"left\":\n", + " event = random.choice([\"potion\", \"trap\"])\n", + "\n", + " if event == \"potion\":\n", + " print(\"You found a potion!\")\n", + " items.append(\"potion\")\n", + "\n", + " else:\n", + " print(\"You fell into a trap and lost 2 health points.\")\n", + " health -= 2\n", + "\n", + " elif choice == \"right\":\n", + " won_battle = encounter_ghost()\n", + "\n", + " if won_battle:\n", + " print(\"The ghost dropped a key!\")\n", + " items.append(\"key\")\n", + " else:\n", + " print(\"The ghost hurt you. You lost 2 health points.\")\n", + " health -= 2\n", + "\n", + " else:\n", + " print(\"Invalid choice. Please choose 'left' or 'right'.\")\n", + " continue\n", + "\n", + " if \"key\" in items:\n", + " print(\"\\nYou unlocked the door and found the Treasure! Congratulations!\")\n", + " break\n", + "\n", + " if health <= 0:\n", + " print(\"\\nGame over, you lost all your health points.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Haunted Mansion!\n", + "\n", + "Your health: 10\n", + "Your items: []\n", + "You fell into a trap and lost 2 health points.\n", + "\n", + "Your health: 8\n", + "Your items: []\n", + "You encounter a ghost!\n", + "You lost the battle...\n", + "The ghost hurt you. You lost 2 health points.\n", + "\n", + "Your health: 6\n", + "Your items: []\n", + "Invalid choice. Please choose 'left' or 'right'.\n", + "\n", + "Your health: 6\n", + "Your items: []\n", + "You fell into a trap and lost 2 health points.\n", + "\n", + "Your health: 4\n", + "Your items: []\n", + "You fell into a trap and lost 2 health points.\n", + "\n", + "Your health: 2\n", + "Your items: []\n", + "Invalid choice. Please choose 'left' or 'right'.\n", + "\n", + "Your health: 2\n", + "Your items: []\n", + "Invalid choice. Please choose 'left' or 'right'.\n", + "\n", + "Your health: 2\n", + "Your items: []\n", + "You fell into a trap and lost 2 health points.\n", + "\n", + "Game over, you lost all your health points.\n" + ] + } + ], + "source": [ + "import random\n", + "\n", "run_mansion()" ] }, @@ -162,7 +272,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -176,7 +286,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.6" } }, "nbformat": 4,