diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..5f09f52 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -77,11 +77,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "499552c8-9e30-46e1-a706-4ac5dc64670e", "metadata": {}, "outputs": [], "source": [ + "import random\n", + "\n", + "# Function to handle the ghost encounter\n", "def encounter_ghost():\n", " \"\"\"\n", " This function handles the encounter with a ghost. \n", @@ -90,15 +93,21 @@ " 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", + "\"\"\"\n", " print(\"You encounter a ghost!\")\n", - " \n", - " # your code goes here" + " battle_outcome = random.randint(1, 10)\n", + " if battle_outcome <= 5:\n", + " print(\"You defeated the ghost!\")\n", + " return \"adventurer_defeated_ghost\"\n", + " else:\n", + " print(\"You lost the battle...\")\n", + " return \"ghost_defeated_adventurer\"\n", + " " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, "outputs": [], @@ -130,7 +139,55 @@ "\n", " \"\"\"\n", " \n", - " # your code goes here" + " health_points = 10\n", + " items = []\n", + " key_found = False\n", + "\n", + " while health_points > 0:\n", + " print(f\"\\nHealth points: {health_points}, Items: {items}\")\n", + " choice = input(\"Choose a path (left/right/use potion): \").strip().lower()\n", + "\n", + " if choice == \"left\":\n", + " event = random.choice([\"potion\", \"trap\"])\n", + " if event == \"potion\":\n", + " items.append(\"potion\")\n", + " print(\"You found a potion! It has been added to your items.\")\n", + " else:\n", + " health_points -= 2\n", + " print(\"You fell into a trap! You lost 2 health points.\")\n", + "\n", + " elif choice == \"right\":\n", + " result = encounter_ghost()\n", + " if result == \"adventurer_defeated_ghost\":\n", + " items.append(\"key\")\n", + " key_found = True\n", + " print(\"You defeated the ghost and found a key!\")\n", + " else:\n", + " health_points -= 2\n", + " print(\"You lost the battle... You lost 2 health points.\")\n", + "\n", + " elif choice == \"use potion\":\n", + " if \"potion\" in items:\n", + " health_points += 5\n", + " items.remove(\"potion\")\n", + " print(\"You used a potion and regained 5 health points.\")\n", + " else:\n", + " print(\"You don't have any potions to use.\")\n", + "\n", + " else:\n", + " print(\"Invalid choice, please try again.\")\n", + " continue\n", + "\n", + " # Game over condition\n", + " if health_points <= 0:\n", + " print(\"\\nGame over, you lost all your health points.\")\n", + " return\n", + "\n", + " # Victory condition if key is found\n", + " if key_found:\n", + " print(\"\\nYou unlocked the door and found the Treasure! Congratulations!\")\n", + " return\n", + "\n" ] }, { @@ -143,12 +200,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Haunted Mansion!\n", + "\n", + "Health points: 10, Items: []\n", + "You encounter a ghost!\n", + "You lost the battle...\n", + "You lost the battle... You lost 2 health points.\n", + "\n", + "Health points: 8, Items: []\n", + "You encounter a ghost!\n", + "You defeated the ghost!\n", + "You defeated the ghost and found a key!\n", + "\n", + "You unlocked the door and found the Treasure! Congratulations!\n" + ] + } + ], "source": [ - "run_mansion()" + "if __name__ == \"__main__\":\n", + " run_mansion()" ] }, { @@ -162,7 +240,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -176,7 +254,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.6" } }, "nbformat": 4,