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
316 changes: 299 additions & 17 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 27,
"id": "cfb2b7b8-e871-4e40-99fe-078c0654c7af",
"metadata": {},
"outputs": [],
"source": [
"possible_items=[\"weapon\",\"key\",\"potion\",]"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "499552c8-9e30-46e1-a706-4ac5dc64670e",
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"def encounter_ghost():\n",
" \"\"\"\n",
" This function handles the encounter with a ghost. \n",
Expand All @@ -92,27 +103,61 @@
" and return something that indicates the ghost defeated the adventurer.\n",
" \"\"\"\n",
" print(\"You encounter a ghost!\")\n",
" \n",
" # your code goes here"
" random_number=random.randint(1,10)\n",
" if random_number<=5:\n",
" print(\"You defeated the Ghost\")\n",
" return \"Winner\"\n",
" else:\n",
" print(\"You lost the Battle\")\n",
" return \"Loser\""
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 47,
"id": "cc1c275c-57fe-49b6-9d1a-f7da09b1aaf9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You encounter a ghost!\n",
"You lost the Battle\n"
]
},
{
"data": {
"text/plain": [
"'Loser'"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"encounter_ghost()"
]
},
{
"cell_type": "code",
"execution_count": 50,
"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",
" \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",
"\n",
" If they choose \"left\", a random event occurs. There is a 50% chance that the adventurer will find a potion and a 50% chance that they will \n",
" If they choose \"left\", a random event occurs. There is a 50% chance that the adventurer will find a portion and a 50% chance that they will \n",
" fall into a trap and lose 2 health points. If they find the potion, it is saved into the adventurer's items. \n",
" If they fall into a trap, 2 points are taken out of the adventurer's health points.\n",
"\n",
Expand All @@ -129,26 +174,255 @@
" If they don't have the key, they are prompted to find it from the beginning.\n",
"\n",
" \"\"\"\n",
" \n",
" # your code goes here"
" random_event=[\"potion\", \"trap\"]\n",
" health_points=10\n",
" adventurers_items=set()\n",
" while health_points>0:\n",
" select_path=input(\"Choose either Left or Right path: \").strip().lower()\n",
" if select_path==\"left\":\n",
" random_choice=random.choice(random_event)\n",
" if random_choice==\"potion\":\n",
" adventurers_items.add(random_choice)\n",
" print(f\"{random_choice} added to adventurers list\")\n",
" elif random_choice==\"trap\":\n",
" health_points-=2\n",
" print(f\"your remaining health point is {health_points}\")\n",
" elif select_path==\"right\":\n",
" outcome=encounter_ghost()\n",
" if outcome==\"Winner\":\n",
" for i in possible_items:\n",
" if i==\"key\":\n",
" adventurers_items.add(i)\n",
" print(\"You unlocked the door and found the treasure, congratulations!!!\")\n",
" print(f\"Your adventurers items are {adventurers_items}\")\n",
" return\n",
" elif outcome==\"Loser\":\n",
" health_points-=2\n",
" print(f\"your remaining health point is {health_points}\")\n",
" else:\n",
" print(\"Please select either left or right path\")\n",
" print(\"Game over!!!, you lost all your health points\")\n",
"\n",
"\n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
"\n",
"\n",
"\n",
" "
]
},
{
"cell_type": "markdown",
"id": "9e13a33c-38e5-44b3-bd1b-9a642c962c89",
"cell_type": "code",
"execution_count": 51,
"id": "f0b77b10-4ada-4efc-8bc0-d01b758aaaf6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the Haunted Mansion!\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Choose either Left or Right path: left\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"your remaining health point is 8\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Choose either Left or Right path: left\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"potion added to adventurers list\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Choose either Left or Right path: left\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"potion added to adventurers list\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Choose either Left or Right path: left\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"potion added to adventurers list\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Choose either Left or Right path: left\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"potion added to adventurers list\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Choose either Left or Right path: left\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"potion added to adventurers list\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Choose either Left or Right path: left\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"potion added to adventurers list\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Choose either Left or Right path: left\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"your remaining health point is 6\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Choose either Left or Right path: left\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"your remaining health point is 4\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Choose either Left or Right path: left\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"your remaining health point is 2\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Choose either Left or Right path: left\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"potion added to adventurers list\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Choose either Left or Right path: left\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"your remaining health point is 0\n",
"Game over!!!, you lost all your health points\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,6 +432,14 @@
"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": "0d627025-070d-4471-a302-dd1f0be1bcb1",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -176,7 +458,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down