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
118 changes: 111 additions & 7 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@
" Good luck!"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "93199b55",
"metadata": {},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c2067779",
"metadata": {},
"outputs": [],
"source": [
"items = [\"weapons\", \"potions\", \"keys\", \"armor\"]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -92,13 +112,21 @@
" and return something that indicates the ghost defeated the adventurer.\n",
" \"\"\"\n",
" print(\"You encounter a ghost!\")\n",
"\n",
" damage = random.randint(1, 10)\n",
" if damage <= 5:\n",
" print(\"You defeated the ghost!\")\n",
" return True\n",
" else:\n",
" print(\"You lost the battle...\")\n",
" return False\n",
" \n",
" # your code goes here"
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -129,8 +157,38 @@
" If they don't have the key, they are prompted to find it from the beginning.\n",
"\n",
" \"\"\"\n",
"\n",
" health = 10\n",
" items = []\n",
"\n",
" while health > 0:\n",
" choice = input(\"Choose between 'left' or 'right': \").lower()\n",
"\n",
" if choice == \"left\":\n",
" event = random.choice([\"potion\", \"trap\"])\n",
" if event == \"potion\":\n",
" print(\"You found a potion!\")\n",
" items.append(\"potion\")\n",
" else:\n",
" print(\"You fell into a trap and lost 2 health points.\")\n",
" health -=2\n",
"\n",
" elif choice == \"right\":\n",
" if encounter_ghost():\n",
" print(\"You find a Key!\")\n",
" items.append(\"key\")\n",
" print(\"You unlocked the door and found the Treasure! Congratulations!\")\n",
" break\n",
"\n",
"\n",
" else:\n",
" print(\"You lost 2 health points.\")\n",
" health -= 2\n",
" else:\n",
" print(\"Invalid choice, please try again.\")\n",
" \n",
" # your code goes here"
" if health <= 0:\n",
" print(\"Game over, you lost all your health points.\")"
]
},
{
Expand All @@ -143,14 +201,60 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the Haunted Mansion!\n",
"You fell into a trap and lost 2 health points.\n",
"You encounter a ghost!\n",
"You lost the battle...\n",
"You lost 2 health points.\n",
"You found a potion!\n",
"You encounter a ghost!\n",
"You defeated the ghost!\n",
"You find a Key!\n",
"You unlocked the door and found the Treasure! Congratulations!\n"
]
}
],
"source": [
"run_mansion()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "afa7a76b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You encounter a ghost!\n",
"You lost the battle...\n"
]
},
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"encounter_ghost()"
]
},
{
"cell_type": "markdown",
"id": "88212f63-3bdb-479f-bf6c-4ecd0685d39a",
Expand All @@ -162,7 +266,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +280,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down