diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..42fb27d 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -77,12 +77,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "id": "499552c8-9e30-46e1-a706-4ac5dc64670e", "metadata": {}, "outputs": [], "source": [ + "import random\n", + "\n", "def encounter_ghost():\n", + " print(\"You encounter a ghost!\")\n", + " outcome = random.randint(1, 10) \n", + " if outcome <= 5:\n", + " print(\"You defeated the ghost!\") \n", + " return True \n", + " else: \n", + " print(\"You lost the battle...\")\n", + " return False \n", + "\n", + "\n", + "\n", + "\n", + "\n", + " \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", @@ -91,22 +107,107 @@ " 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", - " print(\"You encounter a ghost!\")\n", + "\n", " \n", " # your code goes here" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, "outputs": [], "source": [ "# main function for the game\n", + "import random\n", + "\n", + "def use_potion(health, items):\n", + " if \"potion\" not in items:\n", + " print(\"You don't have any potions.\")\n", + " return health, items\n", + "\n", + " drink = input(\"Do you want to use a potion? (yes/no): \").lower()\n", + "\n", + " if drink == \"yes\":\n", + " print(\"You drink the potion and recover 2 health points!\")\n", + " health += 2\n", + " items.remove(\"potion\")\n", + " else:\n", + " print(\"You decide to save the potion for later.\")\n", + "\n", + " return health, items\n", + "\n", + "\n", "def run_mansion():\n", + " health = 10\n", + " items = []\n", " \n", " print(\"Welcome to the Haunted Mansion!\")\n", + " print(\"You start with 10 health points and no items.\\n\")\n", + "\n", + " while health > 0:\n", + " direction = input(\"Choose a path (left or right): \").lower() \n", + " \n", + " while direction not in (\"left\", \"right\"):\n", + " print(\"Invalid choice. Please try again.\")\n", + " direction = input(\"Choose a path (left or right): \").lower()\n", + "\n", + " # LEFT PATH\n", + " if direction == \"left\":\n", + " print(\"\\nYou walk down the left corridor...\")\n", + " event = random.choice([True, False])\n", + "\n", + " if event:\n", + " print(\"You found a potion!\")\n", + " items.append(\"potion\")\n", + " else:\n", + " print(\"You stepped in a trap! You lose 2 health points.\")\n", + " health -= 2\n", + "\n", + " # RIGHT PATH\n", + " elif direction == \"right\":\n", + " print(\"\\nA ghost appears in front of you!\")\n", + " if encounter_ghost():\n", + " print(\"You found a key!\")\n", + " items.append(\"key\")\n", + " else:\n", + " print(\"The ghost drains your energy! You lose 2 health points.\")\n", + " health -= 2\n", + "\n", + " print(f\"\\nCurrent health: {health}\")\n", + " print(f\"Items: {items}\\n\")\n", + " if health <= 0:\n", + " break\n", + " \n", + " # POTION USE OPTION\n", + " if \"potion\" in items:\n", + " use = input(\"Do you want to use a potion? (yes/no): \").lower()\n", + " if use == \"yes\":\n", + " health, items = use_potion(health, items)\n", + " print(f\"Health after potion: {health}\\n\")\n", + "\n", + " # DOOR CHECK\n", + " if \"key\" in items:\n", + " choice = input(\"You have the key! Do you want to try opening the door? (yes/no): \").lower()\n", + " if choice == \"yes\":\n", + " print(\"You unlocked the door and found the Treasure! Congratulations!\")\n", + " return\n", + " else:\n", + " print(\"You decide to keep exploring...\\n\")\n", + "\n", + " print(\"Game over, you lost all your health points.\")\n", + "\n", + "\n", + "\n", + "\n", + " \n", + "\n", + "\n", + "\n", + "\n", + " \n", + " \n", " \n", " \"\"\"\n", " Simulates an adventure through a haunted mansion. The adventurer starts with 10 health points and no items.\n", @@ -143,10 +244,204 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Haunted Mansion!\n", + "You start with 10 health points and no items.\n", + "\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose a path (left or right): left\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "You walk down the left corridor...\n", + "You stepped in a trap! You lose 2 health points.\n", + "\n", + "Current health: 8\n", + "Items: []\n", + "\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to use a potion? (yes/no): yes\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You don't have any potions.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to try opening the door? (yes/no): yes\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The door is locked. You need to find the key first!\n", + "You return to explore the mansion...\n", + "\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose a path (left or right): right\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "A ghost appears in front of you!\n", + "You encounter a ghost!\n", + "You lost the battle...\n", + "The ghost drains your energy! You lose 2 health points.\n", + "\n", + "Current health: 6\n", + "Items: []\n", + "\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to use a potion? (yes/no): yes\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You don't have any potions.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to try opening the door? (yes/no): yes\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The door is locked. You need to find the key first!\n", + "You return to explore the mansion...\n", + "\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose a path (left or right): left\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "You walk down the left corridor...\n", + "You stepped in a trap! You lose 2 health points.\n", + "\n", + "Current health: 4\n", + "Items: []\n", + "\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to use a potion? (yes/no): no\n", + "Do you want to try opening the door? (yes/no): no\n", + "Choose a path (left or right): left\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "You walk down the left corridor...\n", + "You stepped in a trap! You lose 2 health points.\n", + "\n", + "Current health: 2\n", + "Items: []\n", + "\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to use a potion? (yes/no): no\n", + "Do you want to try opening the door? (yes/no): no\n", + "Choose a path (left or right): right\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "A ghost appears in front of you!\n", + "You encounter a ghost!\n", + "You defeated the ghost!\n", + "You found a key!\n", + "\n", + "Current health: 2\n", + "Items: ['key']\n", + "\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to use a potion? (yes/no): no\n", + "Do you want to try opening the door? (yes/no): yes\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You unlocked the door and found the Treasure! Congratulations!\n", + "Game over, you lost all your health points.\n" + ] + } + ], "source": [ "run_mansion()" ] @@ -176,7 +471,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,