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
156 changes: 141 additions & 15 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,15 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 101,
"id": "499552c8-9e30-46e1-a706-4ac5dc64670e",
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"items = [\"poison\", \"axe\", \"key\", \"hat\", \"gun\", \"shield\", \"invisible cloak\"]\n",
"\n",
"def encounter_ghost():\n",
" \"\"\"\n",
" This function handles the encounter with a ghost. \n",
Expand All @@ -93,12 +97,46 @@
" \"\"\"\n",
" print(\"You encounter a ghost!\")\n",
" \n",
" # your code goes here"
" random_number = random.randint(1,10)\n",
" \n",
" if random_number <= 5:\n",
" print(f\"The random number is {random_number}. You defeated the ghost!\")\n",
" return \"win\"\n",
" else:\n",
" print(f\"The random number is {random_number} You lost the battle!\")\n",
" return \"lose\"\n"
]
},
{
"cell_type": "code",
"execution_count": 78,
"id": "4ceae809-4720-4e84-8f12-51ba13b883e3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You encounter a ghost!\n",
"The random number is 3. You defeated the ghost!\n"
]
}
],
"source": [
"encounter_ghost()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c6d7acd5-1d3a-463b-b794-d251fd2bb215",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 117,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"metadata": {},
"outputs": [],
Expand All @@ -107,7 +145,7 @@
"def run_mansion():\n",
" \n",
" print(\"Welcome to the Haunted Mansion!\")\n",
" \n",
"\n",
" \"\"\"\n",
" Simulates an adventure through a haunted mansion. The adventurer starts with 10 health points and no items.\n",
" Prompt the user to choose between two paths: \"left\" or \"right\". \n",
Expand All @@ -130,25 +168,105 @@
"\n",
" \"\"\"\n",
" \n",
" # your code goes here"
" # your code goes here\n",
"\n",
" health_points = 10\n",
" items = []\n",
" \n",
" while health_points > 0: # keep playing until health points runs out\n",
" direction_choice = input(\"Do you want to go left or right? \").lower()\n",
" \n",
" if direction_choice == \"left\":\n",
" random_event = random.choice([\"trap\",\"potion\"])\n",
" print(\"you chose the left!\")\n",
" \n",
" if random_event == \"trap\":\n",
" health_points -= 2\n",
" print(f\"You fell into a trap. You now have {health_points} health points left.\")\n",
" \n",
" # save potion \n",
" else:\n",
" items.append(\"potion\")\n",
" print(f\"You found a potion!\")\n",
" elif direction_choice == \"right\":\n",
" print(\"You chose the right!\")\n",
" outcome = encounter_ghost()\n",
" # defeating the ghost\n",
" if outcome == \"win\":\n",
" items.append(\"key\")\n",
" print(f\"You found a key!. You unlocked the door and found the Treasure! Congratulations!\")\n",
" break\n",
" # the ghost defeats you\n",
" else:\n",
" health_points -= 2\n",
" print(f\"The ghost defeated you. You now have {health_points} health points left.\")\n",
" else:\n",
" print(\"Invalid choice. Please type 'left' or 'right'.\")\n",
"\n",
" # check health points\n",
"\n",
" if health_points <= 0:\n",
" print(\"Game over, you lost all your health points.\")\n",
" break\n",
" \n"
]
},
{
"cell_type": "markdown",
"id": "9e13a33c-38e5-44b3-bd1b-9a642c962c89",
"cell_type": "code",
"execution_count": 116,
"id": "ae3d3ba1-0e50-4196-a560-d13a68571806",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the Haunted Mansion!\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to go left or right? left\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"you chose the left!\n",
"You fell into a trap. You now have 8 health points left.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to go left or right? right\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"You chose the right!\n",
"You encounter a ghost!\n",
"The random number is 1. You defeated the ghost!\n",
"You found a key!. You unlocked the door and found the Treasure! Congratulations!\n"
]
}
],
"source": [
"To run the game, simply call the run_mansion() function:"
"run_mansion()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"cell_type": "markdown",
"id": "9e13a33c-38e5-44b3-bd1b-9a642c962c89",
"metadata": {},
"outputs": [],
"source": [
"run_mansion()"
"To run the game, simply call the run_mansion() function:"
]
},
{
Expand All @@ -158,13 +276,21 @@
"source": [
"This should print the game's narrative and prompt the user to make choices and fight ghosts. The game ends when the adventurer finds the key or loses all their health points. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3740b7f5-9b7f-4e4f-992f-449c4418896a",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -176,7 +302,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down