Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 94 additions & 15 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,16 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "499552c8-9e30-46e1-a706-4ac5dc64670e",
"metadata": {},
"outputs": [],
"source": [
"list_items = [\"bow\", \"shield\", \"armor\", \"spear\", \"helmet\", \"sword\"]\n",
"\n",
"import random\n",
"\n",
"\n",
"def encounter_ghost():\n",
" \"\"\"\n",
" This function handles the encounter with a ghost. \n",
Expand All @@ -91,18 +96,20 @@
" 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",
" r = random.randint(1, 10)\n",
" \n",
" # your code goes here"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"metadata": {},
"outputs": [],
"source": [
"\n",
" if r <= 5:\n",
" print(\"You defeated the ghost!\")\n",
" result = \"Won the battle\"\n",
" else:\n",
" print(\"You lost the battle...\")\n",
" result = \"Lost the battle\"\n",
" return result\n",
" \n",
" # your code goes here\n",
"# main function for the game\n",
"def run_mansion():\n",
" \n",
Expand All @@ -129,7 +136,49 @@
" If they don't have the key, they are prompted to find it from the beginning.\n",
"\n",
" \"\"\"\n",
" \n",
" items = []\n",
" health_points = 10\n",
"\n",
" while health_points > 0:\n",
" while True:\n",
" try:\n",
" p = input(\"Choose a path (left/right): \").lower()\n",
" if p != \"left\" and p != \"right\":\n",
" print(\"Your choice has to be: left or right\")\n",
" break\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter 'left' or 'right'.\")\n",
" if p == \"left\":\n",
" r1 = random.randint(1,2)\n",
" if r1 == 1:\n",
" print(\"You fell into a trap!! Lost 2 HP\")\n",
" health_points -= 2\n",
" elif r1 == 2:\n",
" items.append(\"potion\")\n",
" print(\"You found a potion!\")\n",
" if p == \"right\":\n",
" result = encounter_ghost()\n",
" if result == \"Won the battle\":\n",
" print(\"You found a key!\")\n",
" items.append(\"key\")\n",
" elif result == \"Lost the battle\":\n",
" print(\"Efect of the battle: Lost 2 HP\")\n",
" health_points -= 2\n",
" if \"key\" in items:\n",
" print(\"Now, you found a door to use that key!\")\n",
" print(\"You unlocked the door and found the Treasure! Congratulations!\")\n",
" for item in list_items:\n",
" print(f\"You also found a {item} inside the treasure!\")\n",
" break\n",
" if health_points <= 0:\n",
" print(\"Game over, you lost all your health points.\")\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
" # your code goes here"
]
},
Expand All @@ -146,8 +195,38 @@
"execution_count": null,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the Haunted Mansion!\n",
"Your choice has to be: left or right\n",
"You encounter a ghost!\n",
"You lost the battle...\n",
"Efect of the battle: Lost 2 HP\n",
"Your choice has to be: left or right\n",
"You encounter a ghost!\n",
"You lost the battle...\n",
"Efect of the battle: Lost 2 HP\n",
"Your choice has to be: left or right\n",
"You encounter a ghost!\n",
"You defeated the ghost!\n",
"You found a key!\n",
"Now, you found a door to use that key!\n",
"You unlocked the door and found the Treasure! Congratulations!\n",
"You also found a bow inside the treasure!\n",
"You also found a shield inside the treasure!\n",
"You also found a armor inside the treasure!\n",
"You also found a spear inside the treasure!\n",
"You also found a helmet inside the treasure!\n",
"You also found a sword inside the treasure!\n",
"Game over, you lost all your health points.\n"
]
}
],
"source": [
"\n",
"run_mansion()"
]
},
Expand All @@ -162,7 +241,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +255,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down