diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..66f585b 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": 2, "id": "499552c8-9e30-46e1-a706-4ac5dc64670e", "metadata": {}, "outputs": [], @@ -93,15 +93,74 @@ " \"\"\"\n", " print(\"You encounter a ghost!\")\n", " \n", - " # your code goes here" + " # your code goes here\n", + "\n", + " import random\n", + " result = random.randint(1, 10) # Generate a random number between 1 and 10\n", + " if result <= 5:\n", + " print(\"You defeated the ghost!\")\n", + " return \"Win!\" # Indicating the adventurer defeated the ghost\n", + " else:\n", + " print(\"You lost the battle...\")\n", + " return \"Lose!\" # Indicating the ghost defeated the adventurer\n", + " " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Haunted Mansion ๐Ÿš๏ธ\n", + "Your objective is to find the treasure hidden somewhere inside.\n", + "\n", + "\n", + "Health points: 10\n", + "Inventory: empty\n", + "You fell into a trap and lost 2 health points!\n", + "\n", + "Health points: 8\n", + "Inventory: empty\n", + "You fell into a trap and lost 2 health points!\n", + "\n", + "Health points: 6\n", + "Inventory: empty\n", + "You fell into a trap and lost 2 health points!\n", + "\n", + "Health points: 4\n", + "Inventory: empty\n", + "Invalid choice. Please choose left, right, or forward.\n", + "\n", + "Health points: 4\n", + "Inventory: empty\n", + "\n", + "You encounter a ghost! ๐Ÿ‘ป\n", + "You lost the battle...\n", + "The ghost attacks you. You lose 2 health points.\n", + "\n", + "Health points: 2\n", + "Inventory: empty\n", + "\n", + "You encounter a ghost! ๐Ÿ‘ป\n", + "You defeated the ghost!\n", + "You find a shiny key after defeating the ghost! ๐Ÿ”‘\n", + "\n", + "Health points: 2\n", + "Inventory: key\n", + "\n", + "You encounter a ghost! ๐Ÿ‘ป\n", + "You lost the battle...\n", + "The ghost attacks you. You lose 2 health points.\n", + "\n", + "Game over โ€” you lost all your health points. ๐Ÿ’€\n" + ] + } + ], "source": [ "# main function for the game\n", "def run_mansion():\n", @@ -130,7 +189,110 @@ "\n", " \"\"\"\n", " \n", - " # your code goes here" + " # your code goes here\n", + "\n", + " \n", + "import random\n", + "\n", + "def encounter_ghost():\n", + " \"\"\"\n", + " Handles an encounter with a ghost.\n", + " Returns:\n", + " \"win\" -> the adventurer defeated the ghost.\n", + " \"lose\" -> the ghost defeated the adventurer.\n", + " \"\"\"\n", + " print(\"\\nYou encounter a ghost! ๐Ÿ‘ป\")\n", + " roll = random.randint(1, 10)\n", + "\n", + " if roll <= 5:\n", + " print(\"You defeated the ghost!\")\n", + " return \"win\"\n", + " else:\n", + " print(\"You lost the battle...\")\n", + " return \"lose\"\n", + "\n", + "\n", + "def run_mansion():\n", + " print(\"Welcome to the Haunted Mansion ๐Ÿš๏ธ\")\n", + " print(\"Your objective is to find the treasure hidden somewhere inside.\\n\")\n", + "\n", + " health_points = 10\n", + " items = []\n", + " treasure_found = False\n", + "\n", + " while health_points > 0 and not treasure_found:\n", + " print(f\"\\nHealth points: {health_points}\")\n", + " print(\"Inventory:\", \", \".join(items) if items else \"empty\")\n", + "\n", + " # Ask if the player wants to use a potion\n", + " if \"potion\" in items:\n", + " use_potion = input(\"Do you want to use a potion to heal 2 HP? (yes/no): \").strip().lower()\n", + " if use_potion == \"yes\":\n", + " items.remove(\"potion\")\n", + " health_points += 2\n", + " if health_points > 10:\n", + " health_points = 10\n", + " print(f\"You used a potion. Your health is now {health_points}.\")\n", + " else:\n", + " print(\"You chose not to use a potion.\")\n", + "\n", + " # dd 'forward' only if key exists\n", + " if \"key\" in items:\n", + " choice = input(\"\\nChoose a path (left/right/forward): \").strip().lower()\n", + " else:\n", + " choice = input(\"\\nChoose a path (left/right): \").strip().lower()\n", + "\n", + " # LEFT PATH\n", + " if choice == \"left\":\n", + " event = random.choice([\"potion\", \"trap\"])\n", + " if event == \"potion\":\n", + " print(\"You found a potion! ๐Ÿงช\")\n", + " items.append(\"potion\")\n", + " else:\n", + " print(\"You fell into a trap and lost 2 health points!\")\n", + " health_points -= 2\n", + "\n", + " # RIGHT PATH\n", + " elif choice == \"right\":\n", + " result = encounter_ghost()\n", + " if result == \"win\":\n", + " if \"key\" not in items:\n", + " print(\"You find a shiny key after defeating the ghost! ๐Ÿ”‘\")\n", + " items.append(\"key\")\n", + " else:\n", + " print(\"The ghost disappears. Nothing new happens.\")\n", + " else:\n", + " print(\"The ghost attacks you. You lose 2 health points.\")\n", + " health_points -= 2\n", + "\n", + " # FORWARD PATH (only useful with key)\n", + " elif choice == \"forward\":\n", + " if \"key\" not in items:\n", + " print(\"You find a locked door but you cannot open it. You need a key.\")\n", + " else:\n", + " print(\"You reach a locked door at the end of the mansion...\")\n", + " use_key = input(\"Do you want to use the key to open it? (yes/no): \").strip().lower()\n", + " if use_key == \"yes\":\n", + " print(\"You use the key to open the door...\")\n", + " print(\"You unlocked the door and found the TREASURE! ๐ŸŽโœจ\")\n", + " print(\"Congratulations, you win! ๐ŸŽ‰\")\n", + " treasure_found = True\n", + " else:\n", + " print(\"You decide to explore more before opening the door.\")\n", + "\n", + " else:\n", + " print(\"Invalid choice. Please choose left, right, or forward.\")\n", + " continue\n", + "\n", + " if health_points <= 0:\n", + " print(\"\\nGame over โ€” you lost all your health points. ๐Ÿ’€\")\n", + "\n", + "\n", + "if __name__ == \"__main__\":\n", + " run_mansion()\n", + "\n", + "\n", + "\n" ] }, { @@ -143,10 +305,199 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Haunted Mansion ๐Ÿš๏ธ\n", + "Your objective is to find the treasure hidden somewhere inside.\n", + "\n", + "\n", + "Health points: 10\n", + "Inventory: empty\n", + "\n", + "You encounter a ghost! ๐Ÿ‘ป\n", + "You defeated the ghost!\n", + "You find a shiny key after defeating the ghost! ๐Ÿ”‘\n", + "\n", + "Health points: 10\n", + "Inventory: key\n", + "\n", + "You encounter a ghost! ๐Ÿ‘ป\n", + "You defeated the ghost!\n", + "The ghost disappears. Nothing new happens.\n", + "\n", + "Health points: 10\n", + "Inventory: key\n", + "\n", + "You encounter a ghost! ๐Ÿ‘ป\n", + "You defeated the ghost!\n", + "The ghost disappears. Nothing new happens.\n", + "\n", + "Health points: 10\n", + "Inventory: key\n", + "You found a potion! ๐Ÿงช\n", + "\n", + "Health points: 10\n", + "Inventory: key, potion\n", + "You used a potion. Your health is now 10.\n", + "You found a potion! ๐Ÿงช\n", + "\n", + "Health points: 10\n", + "Inventory: key, potion\n", + "You chose not to use a potion.\n", + "\n", + "You encounter a ghost! ๐Ÿ‘ป\n", + "You defeated the ghost!\n", + "The ghost disappears. Nothing new happens.\n", + "\n", + "Health points: 10\n", + "Inventory: key, potion\n", + "You chose not to use a potion.\n", + "You found a potion! ๐Ÿงช\n", + "\n", + "Health points: 10\n", + "Inventory: key, potion, potion\n", + "You chose not to use a potion.\n", + "\n", + "You encounter a ghost! ๐Ÿ‘ป\n", + "You defeated the ghost!\n", + "The ghost disappears. Nothing new happens.\n", + "\n", + "Health points: 10\n", + "Inventory: key, potion, potion\n", + "You chose not to use a potion.\n", + "You fell into a trap and lost 2 health points!\n", + "\n", + "Health points: 8\n", + "Inventory: key, potion, potion\n", + "You chose not to use a potion.\n", + "\n", + "You encounter a ghost! ๐Ÿ‘ป\n", + "You defeated the ghost!\n", + "The ghost disappears. Nothing new happens.\n", + "\n", + "Health points: 8\n", + "Inventory: key, potion, potion\n", + "You chose not to use a potion.\n", + "\n", + "You encounter a ghost! ๐Ÿ‘ป\n", + "You defeated the ghost!\n", + "The ghost disappears. Nothing new happens.\n", + "\n", + "Health points: 8\n", + "Inventory: key, potion, potion\n", + "You chose not to use a potion.\n", + "\n", + "You encounter a ghost! ๐Ÿ‘ป\n", + "You defeated the ghost!\n", + "The ghost disappears. Nothing new happens.\n", + "\n", + "Health points: 8\n", + "Inventory: key, potion, potion\n", + "You chose not to use a potion.\n", + "\n", + "You encounter a ghost! ๐Ÿ‘ป\n", + "You lost the battle...\n", + "The ghost attacks you. You lose 2 health points.\n", + "\n", + "Health points: 6\n", + "Inventory: key, potion, potion\n", + "You used a potion. Your health is now 8.\n", + "\n", + "You encounter a ghost! ๐Ÿ‘ป\n", + "You defeated the ghost!\n", + "The ghost disappears. Nothing new happens.\n", + "\n", + "Health points: 8\n", + "Inventory: key, potion\n", + "You used a potion. Your health is now 10.\n", + "You fell into a trap and lost 2 health points!\n", + "\n", + "Health points: 8\n", + "Inventory: key\n", + "You found a potion! ๐Ÿงช\n", + "\n", + "Health points: 8\n", + "Inventory: key, potion\n", + "You used a potion. Your health is now 10.\n", + "You fell into a trap and lost 2 health points!\n", + "\n", + "Health points: 8\n", + "Inventory: key\n", + "\n", + "You encounter a ghost! ๐Ÿ‘ป\n", + "You defeated the ghost!\n", + "The ghost disappears. Nothing new happens.\n", + "\n", + "Health points: 8\n", + "Inventory: key\n", + "\n", + "You encounter a ghost! ๐Ÿ‘ป\n", + "You lost the battle...\n", + "The ghost attacks you. You lose 2 health points.\n", + "\n", + "Health points: 6\n", + "Inventory: key\n", + "\n", + "You encounter a ghost! ๐Ÿ‘ป\n", + "You lost the battle...\n", + "The ghost attacks you. You lose 2 health points.\n", + "\n", + "Health points: 4\n", + "Inventory: key\n", + "You found a potion! ๐Ÿงช\n", + "\n", + "Health points: 4\n", + "Inventory: key, potion\n", + "You chose not to use a potion.\n", + "You found a potion! ๐Ÿงช\n", + "\n", + "Health points: 4\n", + "Inventory: key, potion, potion\n", + "You used a potion. Your health is now 6.\n", + "\n", + "You encounter a ghost! ๐Ÿ‘ป\n", + "You lost the battle...\n", + "The ghost attacks you. You lose 2 health points.\n", + "\n", + "Health points: 4\n", + "Inventory: key, potion\n", + "You chose not to use a potion.\n", + "You fell into a trap and lost 2 health points!\n", + "\n", + "Health points: 2\n", + "Inventory: key, potion\n", + "You chose not to use a potion.\n", + "\n", + "You encounter a ghost! ๐Ÿ‘ป\n", + "You defeated the ghost!\n", + "The ghost disappears. Nothing new happens.\n", + "\n", + "Health points: 2\n", + "Inventory: key, potion\n", + "You chose not to use a potion.\n", + "\n", + "You encounter a ghost! ๐Ÿ‘ป\n", + "You defeated the ghost!\n", + "The ghost disappears. Nothing new happens.\n", + "\n", + "Health points: 2\n", + "Inventory: key, potion\n", + "You chose not to use a potion.\n", + "\n", + "You encounter a ghost! ๐Ÿ‘ป\n", + "You lost the battle...\n", + "The ghost attacks you. You lose 2 health points.\n", + "\n", + "Game over โ€” you lost all your health points. ๐Ÿ’€\n" + ] + } + ], "source": [ "run_mansion()" ] @@ -162,7 +513,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -176,7 +527,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,