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
85 changes: 77 additions & 8 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 26,
"id": "499552c8-9e30-46e1-a706-4ac5dc64670e",
"metadata": {},
"outputs": [],
"source": [
"\n",
"import random\n",
"def encounter_ghost():\n",
" \"\"\"\n",
" This function handles the encounter with a ghost. \n",
Expand All @@ -93,17 +95,27 @@
" \"\"\"\n",
" print(\"You encounter a ghost!\")\n",
" \n",
" # your code goes here"
" # your code goes here\n",
"\n",
" number = random.randint(1, 10)\n",
" \n",
" if number <= 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": 27,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"metadata": {},
"outputs": [],
"source": [
"# main function for the game\n",
"import random\n",
"def run_mansion():\n",
" \n",
" print(\"Welcome to the Haunted Mansion!\")\n",
Expand All @@ -130,7 +142,46 @@
"\n",
" \"\"\"\n",
" \n",
" # your code goes here"
" # your code goes here\n",
"\n",
" health = 10\n",
" items = []\n",
" \n",
" while health > 0:\n",
" print(\"Health:\", health)\n",
" print(\"Items:\", items)\n",
" \n",
" choice = input(\"Choose a path (left/right): \").lower()\n",
" \n",
" if choice == \"left\":\n",
" event = random.choice([\"potion\", \"trap\"])\n",
" \n",
" if event == \"potion\":\n",
" print(\"You found a potion!\")\n",
" items.append(\"potion\")\n",
" elif event == \"trap\":\n",
" print(\"You fell into a trap!\")\n",
" health -= 2\n",
" \n",
" elif choice == \"right\":\n",
" won = encounter_ghost()\n",
" \n",
" if won:\n",
" print(\"You found a key!\")\n",
" items.append(\"key\")\n",
" else:\n",
" health -= 2\n",
" \n",
" else:\n",
" print(\"Invalid choice, try again.\")\n",
" \n",
" if \"key\" in items:\n",
" print(\"You unlocked the door and found the Treasure! Congratulations!\")\n",
" break\n",
" \n",
" if health <= 0:\n",
" print(\"Game over, you lost all your health points.\")\n",
" \n"
]
},
{
Expand All @@ -143,10 +194,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 29,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the Haunted Mansion!\n",
"Health: 10\n",
"Items: []\n",
"You encounter a ghost!\n",
"You lost the battle...\n",
"Health: 8\n",
"Items: []\n",
"You encounter a ghost!\n",
"You defeated the ghost!\n",
"You found a key!\n",
"You unlocked the door and found the Treasure! Congratulations!\n"
]
}
],
"source": [
"run_mansion()"
]
Expand All @@ -162,7 +231,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +245,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.2"
}
},
"nbformat": 4,
Expand Down