diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control-extra (2).ipynb similarity index 68% rename from lab-python-flow-control.ipynb rename to lab-python-flow-control-extra (2).ipynb index 7905339..69a6db6 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control-extra (2).ipynb @@ -81,20 +81,7 @@ "id": "499552c8-9e30-46e1-a706-4ac5dc64670e", "metadata": {}, "outputs": [], - "source": [ - "def encounter_ghost():\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", - " If the random number is less than or equal to 5, the adventurer defeats the ghost. In this case, print the message \"You defeated the ghost!\" \n", - " and return something that indicates the adventurer defeated the ghost.\n", - " 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", - " # your code goes here" - ] + "source": "def encounter_ghost():\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 If the random number is less than or equal to 5, the adventurer defeats the ghost. In this case, print the message \"You defeated the ghost!\" \n and return something that indicates the adventurer defeated the ghost.\n 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 import random\n print(\"You encounter a ghost!\")\n \n roll = random.randint(1, 10)\n if roll <= 5:\n print(\"You defeated the ghost!\")\n return True\n else:\n print(\"You lost the battle...\")\n return False" }, { "cell_type": "code", @@ -102,36 +89,7 @@ "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, "outputs": [], - "source": [ - "# main function for the game\n", - "def run_mansion():\n", - " \n", - " print(\"Welcome to the Haunted Mansion!\")\n", - " \n", - " \"\"\"\n", - " Simulates an adventure through a haunted mansion. The adventurer starts with 10 health points and no items.\n", - " Prompt the user to choose between two paths: \"left\" or \"right\". \n", - "\n", - " If they choose \"left\", a random event occurs. There is a 50% chance that the adventurer will find a potion and a 50% chance that they will \n", - " fall into a trap and lose 2 health points. If they find the potion, it is saved into the adventurer's items. \n", - " If they fall into a trap, 2 points are taken out of the adventurer's health points.\n", - "\n", - " If they choose \"right\", the \"encounter_ghost()\" function is called to handle the battle between the adventurer and the ghost. \n", - " If the adventurer wins, they find a key which is saved into the adventurer's items. If they lose, they lose 2 health points.\n", - " Hint: remember to check what encounter_ghost() returned to make know if they won or lost.\n", - "\n", - " If the adventurer chooses something other than \"left\" or \"right\", they are prompted to try again.\n", - "\n", - " If the adventurer's health points reach 0 or less, the message \"Game over, you lost all your health points.\" is printed.\n", - "\n", - " If the adventurer has the key, they can unlock the door and find the Treasure. This message is printed \"You unlocked the door and found the \n", - " Treasure! Congratulations!\".\n", - " If they don't have the key, they are prompted to find it from the beginning.\n", - "\n", - " \"\"\"\n", - " \n", - " # your code goes here" - ] + "source": "# main function for the game\ndef run_mansion():\n import random\n\n print(\"Welcome to the Haunted Mansion!\")\n\n health_points = 10\n items = []\n\n while health_points > 0:\n # Prompt user for path choice\n choice = input(\"\\nChoose a path: 'left' or 'right': \").strip().lower()\n\n if choice == \"left\":\n # 50% chance: find potion or fall into trap\n event = random.randint(1, 2)\n if event == 1:\n print(\"You found a potion! +2 health restored.\")\n items.append(\"potion\")\n health_points += 2\n else:\n print(\"You fell into a trap! You lose 2 health points.\")\n health_points -= 2\n\n elif choice == \"right\":\n # Encounter a ghost\n won = encounter_ghost()\n if won:\n print(\"You found a key!\")\n items.append(\"key\")\n else:\n print(\"You lose 2 health points.\")\n health_points -= 2\n\n else:\n print(\"Invalid choice. Please try again.\")\n continue\n\n print(f\"Health: {health_points} | Items: {items}\")\n\n # Check if health is depleted\n if health_points <= 0:\n print(\"Game over, you lost all your health points.\")\n return\n\n # Check win condition\n if \"key\" in items:\n print(\"\\nYou unlocked the door and found the Treasure! Congratulations!\")\n return\n else:\n print(\"You need to find the key to unlock the treasure door. Keep exploring!\")" }, { "cell_type": "markdown", @@ -147,9 +105,7 @@ "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, "outputs": [], - "source": [ - "run_mansion()" - ] + "source": "run_mansion()" }, { "cell_type": "markdown", @@ -162,7 +118,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -176,9 +132,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.4" } }, "nbformat": 4, "nbformat_minor": 5 -} +} \ No newline at end of file