diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..e55e3aa 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -77,11 +77,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "499552c8-9e30-46e1-a706-4ac5dc64670e", "metadata": {}, "outputs": [], "source": [ + "import random\n", + "\n", "def encounter_ghost():\n", " \"\"\"\n", " This function handles the encounter with a ghost. \n", @@ -93,12 +95,18 @@ " \"\"\"\n", " print(\"You encounter a ghost!\")\n", " \n", - " # your code goes here" + " outcome = random.randint(1, 10)\n", + " if outcome <= 5:\n", + " print(\"You defeated the ghost!\")\n", + " return True\n", + " else:\n", + " print(\"You lost the battle...\")\n", + " return False" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, "outputs": [], @@ -130,7 +138,43 @@ "\n", " \"\"\"\n", " \n", - " # your code goes here" + " health = 10\n", + " items = []\n", + "\n", + " while health > 0:\n", + " choice = input(\"You are at a fork. 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! You lose 2 health points.\")\n", + " health -= 2\n", + " print(f\"Current health: {health}\")\n", + " elif choice == \"right\":\n", + " result = encounter_ghost()\n", + " if result:\n", + " print(\"You found a key after defeating the ghost!\")\n", + " items.append(\"key\")\n", + " else:\n", + " print(\"The ghost hurt you! You lose 2 health points.\")\n", + " health -= 2\n", + " print(f\"Current health: {health}\")\n", + " else:\n", + " print(\"Invalid choice. Please choose 'left' or 'right'.\")\n", + " continue\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\n", + " else:\n", + " print(\"You need to find the key to unlock the treasure. Try again.\")" ] }, { @@ -143,10 +187,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Haunted Mansion!\n" + ] + } + ], "source": [ "run_mansion()" ] @@ -162,7 +214,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "venv", "language": "python", "name": "python3" }, @@ -176,7 +228,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,