From d74a61f724a8b28d645ab10f92608a36177072e9 Mon Sep 17 00:00:00 2001 From: carmenlnr Date: Tue, 14 Apr 2026 17:43:33 +0200 Subject: [PATCH] Update lab-python-flow-control.ipynb --- lab-python-flow-control.ipynb | 105 +++++++++++++++++++++++++++++++--- 1 file changed, 97 insertions(+), 8 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..9c32ec3 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -80,7 +80,26 @@ "execution_count": null, "id": "499552c8-9e30-46e1-a706-4ac5dc64670e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You encounter a ghost!\n", + "You lost the battle...\n" + ] + }, + { + "data": { + "text/plain": [ + "'the ghost defeated the adventurer'" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def encounter_ghost():\n", " \"\"\"\n", @@ -93,7 +112,15 @@ " \"\"\"\n", " print(\"You encounter a ghost!\")\n", " \n", - " # your code goes here" + " # your code goes here\n", + " random_number = int(input(\"Introduce a number between 1 and 10:\"))\n", + "\n", + " if random_number <=5:\n", + " print (\"You defeated the ghost!\")\n", + " return \"the adventurer defeated the ghost\"\n", + " else:\n", + " print (\"You lost the battle...\")\n", + " return \"the ghost defeated the adventurer\"\n" ] }, { @@ -101,7 +128,20 @@ "execution_count": null, "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Haunted Mansion!\n", + "Try again from the beginning.\n", + "Invalid path, try again!\n", + "Try again from the beginning.\n", + "Invalid path, try again!\n", + "Try again from the beginning.\n" + ] + } + ], "source": [ "# main function for the game\n", "def run_mansion():\n", @@ -130,7 +170,44 @@ "\n", " \"\"\"\n", " \n", - " # your code goes here" + " # your code goes here\n", + " health_points = 10\n", + " items = []\n", + "\n", + " while health_points > 0:\n", + "\n", + " choice = input(\"left or right?\")\n", + "\n", + " if choice == \"left\":\n", + " event = input (\"potion or trap? \")\n", + "\n", + " if event == \"potion\":\n", + " items.append(\"potion\")\n", + " else:\n", + " health_points -=2 \n", + "\n", + " elif choice == \"right\":\n", + " result = encounter_ghost()\n", + "\n", + " if result == \"the adventurer defeated the ghost\":\n", + " items.append(\"key\")\n", + " else:\n", + " health_points -= 2\n", + " \n", + " else:\n", + " print (\"Invalid path, try again!\") \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 (\"Try again from the beginning.\")\n", + "\n", + "run_mansion()" ] }, { @@ -143,10 +220,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'run_mansion' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[1]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m run_mansion()\n", + "\u001b[31mNameError\u001b[39m: name 'run_mansion' is not defined" + ] + } + ], "source": [ "run_mansion()" ] @@ -162,7 +251,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -176,7 +265,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.3" } }, "nbformat": 4,