From a930cf3282ca3bcca61692517a3488115a2a75d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my-Morgan=20PATOLE?= Date: Thu, 9 Oct 2025 18:10:59 +0200 Subject: [PATCH] lab done --- lab-python-flow-control.ipynb | 82 +++++++++++++++++++++++++++++++---- 1 file changed, 74 insertions(+), 8 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..6edec81 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,22 @@ " \"\"\"\n", " print(\"You encounter a ghost!\")\n", " \n", - " # your code goes here" + " # your code goes here\n", + " import random\n", + " \n", + " battle_outcome = random.randint(1, 10)\n", + " \n", + " if battle_outcome <= 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": 9, "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, "outputs": [], @@ -130,7 +140,42 @@ "\n", " \"\"\"\n", " \n", - " # your code goes here" + " # your code goes here\n", + " import random\n", + " health_points = 10\n", + " adventurer_items = []\n", + "\n", + " while health_points > 0 and \"key\" not in adventurer_items:\n", + " choice = input(\"Do you want to go left or right? \").lower()\n", + " if choice == \"left\":\n", + " print(\"You chose to go left.\")\n", + " print(\"A random event is happening...\")\n", + " event = random.randint(1, 2)\n", + " if event == 1:\n", + " print(\"You found a potion!\")\n", + " adventurer_items.append(\"potion\")\n", + " else:\n", + " health_points -= 2\n", + " print(\"You fell into a trap and lost 2 health points.\")\n", + " print(f\"Your current health points: {health_points}\")\n", + " print(\"You need to find the key to unlock the door.\")\n", + " elif choice == \"right\":\n", + " print(\"You chose to go right.\")\n", + " if encounter_ghost() == True:\n", + " print(\"You found a key!\")\n", + " adventurer_items.append(\"key\")\n", + " else:\n", + " health_points -= 2\n", + " print(\"You lost 2 health points.\")\n", + " print(f\"Your current health points: {health_points}\")\n", + " else:\n", + " print(\"Invalid choice, please try again.\")\n", + "\n", + " if health_points <= 0:\n", + " print(\"Game over, you lost all your health points.\")\n", + " elif \"key\" in adventurer_items:\n", + " print(\"You unlocked the door and found the Treasure! Congratulations!\")\n", + " \n" ] }, { @@ -143,10 +188,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Haunted Mansion!\n", + "You chose to go left.\n", + "A random event is happening...\n", + "You found a potion!\n", + "You chose to go right.\n", + "You encounter a ghost!\n", + "You lost the battle...\n", + "You lost 2 health points.\n", + "Your current health points: 8\n", + "You chose to go right.\n", + "You encounter a ghost!\n", + "You defeated the ghost!\n", + "You found a key!\n", + "You unlocked the door and found the Treasure! Congratulations!\n" + ] + } + ], "source": [ "run_mansion()" ] @@ -162,7 +228,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -176,7 +242,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.6" } }, "nbformat": 4,