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
126 changes: 118 additions & 8 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "499552c8-9e30-46e1-a706-4ac5dc64670e",
"metadata": {},
"outputs": [],
Expand All @@ -93,12 +93,12 @@
" \"\"\"\n",
" print(\"You encounter a ghost!\")\n",
" \n",
" # your code goes here"
" import random"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -130,7 +130,19 @@
"\n",
" \"\"\"\n",
" \n",
" # your code goes here"
" import random\n",
"\n",
"def encounter_ghost():\n",
" print(\"You encounter a ghost!\")\n",
"\n",
" random_number = random.randint(1, 10)\n",
"\n",
" if random_number <= 5:\n",
" print(\"You defeated the ghost!\")\n",
" return True\n",
" else:\n",
" print(\"You lost the battle...\")\n",
" return False"
]
},
{
Expand All @@ -143,11 +155,109 @@
},
{
"cell_type": "code",
"execution_count": null,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"execution_count": 5,
"id": "71294f56",
"metadata": {},
"outputs": [],
"source": [
"def run_mansion():\n",
" print(\"Welcome to the Haunted Mansion!\")\n",
"\n",
" health = 10\n",
" items = []\n",
"\n",
" while health > 0:\n",
" print(\"\\nYour health:\", health)\n",
" print(\"Your items:\", items)\n",
"\n",
" choice = input(\"Choose a path: left or 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",
"\n",
" else:\n",
" print(\"You fell into a trap and lost 2 health points.\")\n",
" health -= 2\n",
"\n",
" elif choice == \"right\":\n",
" won_battle = encounter_ghost()\n",
"\n",
" if won_battle:\n",
" print(\"The ghost dropped a key!\")\n",
" items.append(\"key\")\n",
" else:\n",
" print(\"The ghost hurt you. You lost 2 health points.\")\n",
" health -= 2\n",
"\n",
" else:\n",
" print(\"Invalid choice. Please choose 'left' or 'right'.\")\n",
" continue\n",
"\n",
" if \"key\" in items:\n",
" print(\"\\nYou unlocked the door and found the Treasure! Congratulations!\")\n",
" break\n",
"\n",
" if health <= 0:\n",
" print(\"\\nGame over, you lost all your health points.\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the Haunted Mansion!\n",
"\n",
"Your health: 10\n",
"Your items: []\n",
"You fell into a trap and lost 2 health points.\n",
"\n",
"Your health: 8\n",
"Your items: []\n",
"You encounter a ghost!\n",
"You lost the battle...\n",
"The ghost hurt you. You lost 2 health points.\n",
"\n",
"Your health: 6\n",
"Your items: []\n",
"Invalid choice. Please choose 'left' or 'right'.\n",
"\n",
"Your health: 6\n",
"Your items: []\n",
"You fell into a trap and lost 2 health points.\n",
"\n",
"Your health: 4\n",
"Your items: []\n",
"You fell into a trap and lost 2 health points.\n",
"\n",
"Your health: 2\n",
"Your items: []\n",
"Invalid choice. Please choose 'left' or 'right'.\n",
"\n",
"Your health: 2\n",
"Your items: []\n",
"Invalid choice. Please choose 'left' or 'right'.\n",
"\n",
"Your health: 2\n",
"Your items: []\n",
"You fell into a trap and lost 2 health points.\n",
"\n",
"Game over, you lost all your health points.\n"
]
}
],
"source": [
"import random\n",
"\n",
"run_mansion()"
]
},
Expand All @@ -162,7 +272,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +286,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down