From e60e5548195a7af0fa4507ac2a0168e065073b79 Mon Sep 17 00:00:00 2001 From: Julia Date: Sat, 6 Sep 2025 15:12:20 +0100 Subject: [PATCH] Solved Lab --- lab-python-flow-control.ipynb | 371 +++++++++++++++++++++++++++++++++- 1 file changed, 364 insertions(+), 7 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..2d874b2 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -77,7 +77,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, + "id": "6c152292-f729-44e1-a232-e9b103827777", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9\n" + ] + } + ], + "source": [ + "import random\n", + "print(random.randint(1, 10))" + ] + }, + { + "cell_type": "code", + "execution_count": 54, "id": "499552c8-9e30-46e1-a706-4ac5dc64670e", "metadata": {}, "outputs": [], @@ -93,12 +112,52 @@ " \"\"\"\n", " print(\"You encounter a ghost!\")\n", " \n", - " # your code goes here" + " # your code goes here\n", + "\n", + " import random\n", + " battle_outcome = random.randint(1, 10)\n", + " if battle_outcome <= 5:\n", + " print(\"You defeated the ghost!\")\n", + " result = True\n", + " elif battle_outcome > 5:\n", + " print(\"You lost the battle...\")\n", + " result = False\n", + "\n", + " return result" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, + "id": "c3472810-c9a8-4615-ae75-1472707df379", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You encounter a ghost!\n", + "You lost the battle...\n" + ] + }, + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "encounter_ghost()" + ] + }, + { + "cell_type": "code", + "execution_count": 58, "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, "outputs": [], @@ -130,7 +189,38 @@ "\n", " \"\"\"\n", " \n", - " # your code goes here" + " # your code goes here\n", + "\n", + " health_points = 10\n", + " print(f\"You have {health_points} health points!\")\n", + " items = []\n", + " import random\n", + " while health_points > 0:\n", + " path = input(\"Choose left or right.\").lower()\n", + " if path == \"left\":\n", + " path_left_result = random.randint(1, 2)\n", + " if path_left_result == 1:\n", + " print(\"You have found a potion!\")\n", + " items.append(\"potion\")\n", + " if path_left_result == 2:\n", + " print(\"You fell into a trap and lost 2 health points!\")\n", + " health_points -= 2\n", + " elif path == \"right\":\n", + " encounter_ghost_var = encounter_ghost()\n", + " if encounter_ghost_var == True:\n", + " print(\"You have found a key!\")\n", + " items.append(\"key\")\n", + " elif encounter_ghost_var == False:\n", + " print(\"You lost 2 health points!\")\n", + " health_points -= 2\n", + " else:\n", + " print(\"Invalid choice!\")\n", + " if health_points <= 0:\n", + " print(\"Game over, you lost all your health points.\")\n", + " if \"key\" in items:\n", + " print(\"You unlocked the door and found the Treasure! Congratulations!\")\n", + " else:\n", + " health_points = 10" ] }, { @@ -143,10 +233,277 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 59, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Haunted Mansion!\n", + "You have 10 health points!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose left or right. left\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You fell into a trap and lost 2 health points!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose left or right. leftt\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid choice!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose left or right. left\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You have found a potion!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose left or right. right\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You encounter a ghost!\n", + "You defeated the ghost!\n", + "You have found a key!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose left or right. left\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You have found a potion!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose left or right. right\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You encounter a ghost!\n", + "You defeated the ghost!\n", + "You have found a key!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose left or right. right\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You encounter a ghost!\n", + "You lost the battle...\n", + "You lost 2 health points!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose left or right. right\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You encounter a ghost!\n", + "You lost the battle...\n", + "You lost 2 health points!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose left or right. right\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You encounter a ghost!\n", + "You lost the battle...\n", + "You lost 2 health points!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose left or right. left\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You have found a potion!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose left or right. rightt\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid choice!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose left or right. left\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You have found a potion!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose left or right. right\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You encounter a ghost!\n", + "You defeated the ghost!\n", + "You have found a key!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose left or right. right\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You encounter a ghost!\n", + "You defeated the ghost!\n", + "You have found a key!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose left or right. right\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You encounter a ghost!\n", + "You defeated the ghost!\n", + "You have found a key!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose left or right. right\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You encounter a ghost!\n", + "You defeated the ghost!\n", + "You have found a key!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose left or right. left\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You fell into a trap and lost 2 health points!\n", + "Game over, you lost all your health points.\n", + "You unlocked the door and found the Treasure! Congratulations!\n" + ] + } + ], "source": [ "run_mansion()" ] @@ -176,7 +533,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,