diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..7e52d4a 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -77,28 +77,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 96, "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" + " print(\"The battle begins!\")\n", + "\n", + " number = input(\"Choose a number to determine the outcome of the battle (1-6): \")\n", + " if not number.isdigit():\n", + " print(\"That's not a valid number. The ghost took advantage of your hesitation!\")\n", + " return False\n", + "\n", + " number = int(number)\n", + " if number <= 3:\n", + " print(\"You defeated the ghost!\")\n", + " return True\n", + "\n", + " else:\n", + " print(\"You lost the battle...\")\n", + " return False" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 97, + "id": "5d72ca1d", + "metadata": {}, + "outputs": [], + "source": [ + "number = int(number)" + ] + }, + { + "cell_type": "code", + "execution_count": 98, "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, "outputs": [], @@ -108,29 +123,92 @@ " \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", + " health_points = 10\n", + " items = []\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", + " print(\"\\n You have\", health_points, \"health points, and your items are:\", items)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cd0f5999", + "metadata": {}, + "outputs": [], + "source": [ + "def run_mansion():\n", + " print(\"Welcome to the Haunted Mansion!\")\n", + " \n", + " health_points = 10\n", + " items = []\n", "\n", - " If the adventurer chooses something other than \"left\" or \"right\", they are prompted to try again.\n", + " print(\"\\n You have\", health_points, \"health points, and your items are:\", items)\n", + " \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", + " while health_points > 0:\n", + " path = input(\"\\nYou see two paths ahead: left and right. Where do you go? (left/right): \").lower()\n", + " if path == \"left\":\n", + " print(\"\\nYou chose the left path and found a misteryous locked chest!\")\n", + " fate = input(\"\\nChoose a number between 1 and 10 to open the chest: \")\n", + " if not fate.isdigit():\n", + " print(\"That's not a valid number. The chest remains locked.\")\n", + " else:\n", + " fate = int(fate)\n", + " if fate >= 7:\n", + " print(\"You found a longhammer inside the chest!\")\n", + " items.append(\"longhammer\")\n", + " else:\n", + " print(\"The chest exploded, you lost 1 health points!\")\n", + " health_points -= 1\n", + " if health_points <= 0:\n", + " print(\"Game over, you died!\")\n", + " break\n", + " elif path == \"right\":\n", + " print(\"\\nYou chose the right path and encountered a ghost!\")\n", + " result = encounter_ghost()\n", + " if result:\n", + " print(\"Battle was won and you stole the ghost's soul!\")\n", + " if \"soul\" not in items:\n", + " items.append(\"soul\")\n", + " else:\n", + " print(\"You already have the soul.\") \n", + " else:\n", + " print(\"The ghost striked you! You lost 1 health point.\")\n", + " print(\"You fleed in terror to the mansion entrance.\")\n", + " health_points -= 1\n", + " print(\"\\n You have\", health_points, \"health points\")\n", + " if health_points <= 0:\n", + " print(\"Game over, you died!\")\n", + " break\n", + " else:\n", + " continue\n", + " else:\n", + " print(\"Invalid choice. Please choose 'left' or 'right'.\")\n", + " continue\n", + " print(\"\\n You have\", health_points, \"health points\")\n", + " print(\"Your items are:\", items)\n", + " if \"soul\" in items:\n", + " print(\"You move further trough darkness again ...\")\n", + " print(\"You reached a tall wooden door and opened it.\")\n", + " print(\"You entered a room filled with darkness all around you but you manage to see this weak stonewall glittering in the dark.\")\n", + " found_treasure = False\n", + " \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", + " if \"longhammer\" in items:\n", + " print(\"\\nYou combined the longhammer and the ghost's soul power!\")\n", + " print(\"With a mighty swing, you smashed the stonewall and found a treasure!!\")\n", + " found_treasure = True\n", + " print(\"You collected the treasure and escaped the mansion!! Congratulations, you won the game!\")\n", + " break\n", + " else:\n", + " print(\"\\nYou tried to break the wall, but your hands are too weak.\")\n", + " print(\"You need more power to break the stonewall.\")\n", + " print(\"You returned to the mansion entrance to search again...\")\n", + " continue\n", + " \n", "\n", - " \"\"\"\n", - " \n", - " # your code goes here" + "\n" ] }, { @@ -143,10 +221,54 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 102, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Haunted Mansion!\n", + "\n", + " You have 10 health points, and your items are: []\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "You chose the right path and encountered a ghost!\n", + "The battle begins!\n", + "You defeated the ghost!\n", + "Battle was won and you stole the ghost's soul!\n", + "\n", + " You have 10 health points\n", + "Your items are: ['soul']\n", + "You moved further again ...\n", + "You reached a tall wooden door and opened it.\n", + "You entered a room filled with darkness all around you but you manage to see this weak stonewall glittering in the dark.\n", + "\n", + "You tried to break the wall, but your hands are too weak.\n", + "You need more power to break the stonewall.\n", + "You returned to the mansion entrance to search again...\n", + "\n", + "You chose the left path and found a misteryous locked chest!\n", + "You found a longhammer inside the chest!\n", + "\n", + " You have 10 health points\n", + "Your items are: ['soul', 'longhammer']\n", + "You moved further again ...\n", + "You reached a tall wooden door and opened it.\n", + "You entered a room filled with darkness all around you but you manage to see this weak stonewall glittering in the dark.\n", + "\n", + "You combined the longhammer and the ghost's soul power!\n", + "With a mighty swing, you smashed the stonewall and found a treasure!!\n", + "You collected the treasure and escaped the mansion!! Congratulations, you won the game!\n" + ] + } + ], "source": [ "run_mansion()" ] @@ -162,7 +284,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -176,7 +298,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,