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
87 changes: 77 additions & 10 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": 4,
"id": "499552c8-9e30-46e1-a706-4ac5dc64670e",
"metadata": {},
"outputs": [],
Expand All @@ -86,19 +86,29 @@
" \"\"\"\n",
" This function handles the encounter with a ghost. \n",
" The outcome of the battle is determined by a random number between 1 and 10.\n",
" If the random number is less than or equal to 5, the adventurer defeats the ghost. In this case, print the message \"You defeated the ghost!\" \n",
" If the random number is less than or equal to 5, the adventurer defeats the ghost.\n",
" In this case, print the message \"You defeated the ghost!\" \n",
" and return something that indicates the adventurer defeated the ghost.\n",
" If the random number is greater than 5, the adventurer loses the battle. In this case, print \"You lost the battle...\"\n",
" and return something that indicates the ghost defeated the adventurer.\n",
" \"\"\"\n",
" print(\"You encounter a ghost!\")\n",
" \n",
" # your code goes here"
" # your code goes here\n",
"\n",
" import random\n",
" outcome = random.randint(1, 10)\n",
" if outcome <= 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": 5,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"metadata": {},
"outputs": [],
Expand All @@ -124,13 +134,46 @@
"\n",
" If the adventurer's health points reach 0 or less, the message \"Game over, you lost all your health points.\" is printed.\n",
"\n",
" If the adventurer has the key, they can unlock the door and find the Treasure. This message is printed \"You unlocked the door and found the \n",
" If the adventurer has the key, they can unlock the door and find the Treasure.\n",
" This message is printed \"You unlocked the door and found the \n",
" Treasure! Congratulations!\".\n",
" If they don't have the key, they are prompted to find it from the beginning.\n",
"\n",
" \"\"\"\n",
" \n",
" # your code goes here"
" # your code goes here\n",
" import random\n",
" health_points = 10\n",
" items = []\n",
" while health_points > 0 and \"key\" not in items:\n",
" first_prompt = input(\"You are in a haunted mansion. Do you want to go left or right? \").lower()\n",
" if first_prompt == \"left\":\n",
" left_path = random.randint(1, 10)\n",
" if left_path <= 5:\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_points -= 2\n",
" elif first_prompt == \"right\":\n",
" battle_outcome = encounter_ghost()\n",
" if battle_outcome == True:\n",
" print(\"You found a key!\")\n",
" items.append(\"key\")\n",
" else:\n",
" print(\"You lost 2 health points.\")\n",
" health_points -= 2\n",
" else:\n",
" print(\"Invalid choice. Please try again.\")\n",
" return run_mansion()\n",
" \n",
" if health_points <= 0:\n",
" print(\"Game over, you lost all your health points.\")\n",
" elif \"key\" in items:\n",
" print(\"You unlocked the door and found the Treasure! Congratulations!\")\n",
" else:\n",
" print(\"You need to find the key to unlock the door. Start again.\")\n",
" return run_mansion()\n"
]
},
{
Expand All @@ -143,10 +186,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"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 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 @@ -158,11 +217,19 @@
"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": "c7dcee06",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +243,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down