diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..48dee96 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": 4, "id": "499552c8-9e30-46e1-a706-4ac5dc64670e", "metadata": {}, "outputs": [], @@ -93,12 +93,32 @@ " \"\"\"\n", " print(\"You encounter a ghost!\")\n", " \n", - " # your code goes here" + "import random\n", + "\n", + "def encounter_ghost(items):\n", + " \"\"\"\n", + " Handles the encounter with a ghost.\n", + " Returns True if adventurer defeats the ghost, False otherwise.\n", + " Adventurer can use a weapon if available for a better chance.\n", + " \"\"\"\n", + " print(\"You encounter a ghost!\")\n", + " use_weapon = \"weapon\" in items\n", + " chance = random.randint(1, 10)\n", + " if use_weapon:\n", + " print(\"You use your weapon against the ghost!\")\n", + " chance += 2 # Better chance if weapon\n", + " if chance <= 5:\n", + " print(\"You defeated the ghost!\")\n", + " return True\n", + " else:\n", + " print(\"You lost the battle...\")\n", + " return False\n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, "outputs": [], @@ -129,8 +149,45 @@ " If they don't have the key, they are prompted to find it from the beginning.\n", "\n", " \"\"\"\n", - " \n", - " # your code goes here" + "import random\n", + "def run_mansion():\n", + " print(\"Welcome to the Haunted Mansion!\")\n", + " health = 10\n", + " items = []\n", + "\n", + " while health > 0:\n", + " choice = input(\"You reach an intersection. Do you go 'left' or 'right'? \").lower()\n", + " if choice == \"left\":\n", + " event = random.choice([\"potion\", \"trap\"])\n", + " if event == \"potion\":\n", + " print(\"You found a potion! Your health increases by 2.\")\n", + " items.append(\"potion\")\n", + " health += 2\n", + " else:\n", + " print(\"You fell into a trap and lost 2 health points.\")\n", + " health -= 2\n", + " elif choice == \"right\":\n", + " result = encounter_ghost(items)\n", + " if result:\n", + " print(\"You find a key after defeating the ghost!\")\n", + " items.append(\"key\")\n", + " else:\n", + " print(\"You lose 2 health points.\")\n", + " health -= 2\n", + " else:\n", + " print(\"Invalid choice. Please choose 'left' or 'right'.\")\n", + " continue\n", + "\n", + " print(f\"Current health: {health}\")\n", + " print(f\"Items: {items}\")\n", + "\n", + " if health <= 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" ] }, { @@ -143,10 +200,49 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Haunted Mansion!\n", + "You found a potion! Your health increases by 2.\n", + "Current health: 12\n", + "Items: ['potion']\n", + "You encounter a ghost!\n", + "You lost the battle...\n", + "You lose 2 health points.\n", + "Current health: 10\n", + "Items: ['potion']\n", + "You encounter a ghost!\n", + "You lost the battle...\n", + "You lose 2 health points.\n", + "Current health: 8\n", + "Items: ['potion']\n", + "You fell into a trap and lost 2 health points.\n", + "Current health: 6\n", + "Items: ['potion']\n", + "You fell into a trap and lost 2 health points.\n", + "Current health: 4\n", + "Items: ['potion']\n", + "You fell into a trap and lost 2 health points.\n", + "Current health: 2\n", + "Items: ['potion']\n", + "You found a potion! Your health increases by 2.\n", + "Current health: 4\n", + "Items: ['potion', 'potion']\n", + "You encounter a ghost!\n", + "You defeated the ghost!\n", + "You find a key after defeating the ghost!\n", + "Current health: 4\n", + "Items: ['potion', 'potion', 'key']\n", + "You unlocked the door and found the Treasure! Congratulations!\n" + ] + } + ], "source": [ "run_mansion()" ] @@ -162,7 +258,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -176,7 +272,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,