Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solved Lab #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
101 changes: 82 additions & 19 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,60 +77,110 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "722ede3e",
"metadata": {},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "499552c8-9e30-46e1-a706-4ac5dc64670e",
"metadata": {},
"outputs": [],
"source": [
"def encounter_ghost():\n",
" \"\"\"\n",
" This function handles the encounter with a ghost. \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. 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"
"\n",
" num = random.randint(1,10)\n",
" # your code goes here\n",
" if num <= 5:\n",
" print(\"You defeated the ghost!\")\n",
" return \"won\"\n",
" else:\n",
" print(\"You lost the battle...\")\n",
" return \"lost\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"metadata": {},
"outputs": [],
"source": [
"# main function for the game\n",
"def run_mansion():\n",
" \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",
" 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",
" 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 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",
" 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",
" If they choose \"right\", the \"encounter_ghost()\" function is called to handle the battle between the adventurer and the ghost. \n",
" If they choose \"right\", the \"encounter_ghost()\" function is called to handle the battle between the adventurer and the ghost.\n",
" If the adventurer wins, they find a key which is saved into the adventurer's items. If they lose, they lose 2 health points.\n",
" Hint: remember to check what encounter_ghost() returned to make know if they won or lost.\n",
"\n",
" If the adventurer chooses something other than \"left\" or \"right\", they are prompted to try again.\n",
"\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. 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"
"\n",
" # your code goes here\n",
" health_points = 10\n",
" items = []\n",
"\n",
" while health_points > 0:\n",
" path_user = input('Choose between two paths: \"left\" or \"right\"').lower()\n",
"\n",
" if path_user == \"left\":\n",
" if random.choice([True,False]):\n",
" print(\"You find 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",
"\n",
" elif path_user == \"right\":\n",
" battle = encounter_ghost()\n",
" if battle ==\"won\":\n",
" items.append(\"key\")\n",
" elif battle == \"lost\":\n",
" health_points -= 2\n",
"\n",
" else:\n",
" print(\"Try again\")\n",
"\n",
" # Check if the adventurer has the key to unlock the door\n",
" if \"key\" in items:\n",
" print(\"You unlocked the door and found the Treasure! Congratulations!\")\n",
" return\n",
"\n",
" # If health points drop to 0 or below\n",
" if health_points <= 0:\n",
" print(\"Game over, you lost all your health points.\")\n",
" return\n"
]
},
{
Expand All @@ -143,10 +193,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"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 fell into a trap and lost 2 health points.\n",
"You encounter a ghost!\n",
"You defeated the ghost!\n",
"You unlocked the door and found the Treasure! Congratulations!\n"
]
}
],
"source": [
"run_mansion()"
]
Expand All @@ -162,7 +225,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +239,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.1"
}
},
"nbformat": 4,
Expand Down