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
122 changes: 111 additions & 11 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,20 @@
"execution_count": null,
"id": "499552c8-9e30-46e1-a706-4ac5dc64670e",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You encounter a ghost!\n",
"You defeated the ghost!\n",
"The result of the battle is: Win\n"
]
}
],
"source": [
"import random\n",
"\n",
"def encounter_ghost():\n",
" \"\"\"\n",
" This function handles the encounter with a ghost. \n",
Expand All @@ -93,21 +105,33 @@
" \"\"\"\n",
" print(\"You encounter a ghost!\")\n",
" \n",
" random_number = int(input(\"To fight the ghost you must enter a number between 1 and 10: \"))\n",
" \n",
" if random_number > 5:\n",
" print(\"You lost the battle\")\n",
" return \"Lose\"\n",
" \n",
" else:\n",
" print(\"You defeated the ghost!\")\n",
" return \"Win\"\n",
"\n",
"result = encounter_ghost()\n",
"\n",
"\n",
" # your code goes here"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"id": "2c8da343",
"metadata": {},
"outputs": [],
"source": [
"# main function for the game\n",
"\n",
"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 @@ -129,8 +153,52 @@
" 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",
" \n",
" import random\n",
"\n",
" print(\"Welcome to the Haunted Mansion!\")\n",
" print(\"The adventure starts now! You have 10 health points and no items in your pocket.\\nYou will navigate through the mansion choosing different options and you may encounter some surprises...!\")\n",
"\n",
" health_points = 10\n",
" pocket_items = [] \n",
"\n",
" \n",
" while health_points > 0 and \"key\" not in pocket_items:\n",
" choose_path = input(\"You're walking, but... which path should you follow, right or left?: \").lower()\n",
" \n",
" if choose_path == \"left\":\n",
" options = [\"Congrats! You found a potion!\", \"Ohh you falled into a trap...!\"]\n",
" answer = random.choice(options)\n",
" print(answer)\n",
" if answer == \"Congrats! You found a potion!\":\n",
" pocket_items.append(\"Potion\")\n",
" print(\"Now you have one item in your pocket\")\n",
" else:\n",
" health_points -= 2\n",
" print(f\"You've lost 2 health points: {health_points} remaining.\")\n",
" if health_points <= 0:\n",
" print(\"Game Over. You lost all your health points.\")\n",
" else:\n",
" print(\"Keep walking to find the treasure!\")\n",
" elif choose_path == \"right\":\n",
" result = encounter_ghost() \n",
" if result == \"Win\":\n",
" pocket_items.append(\"key\")\n",
" print(\"You found a key and unlocked the door. There's the Treasure! Congratulations!\")\n",
" break\n",
" else:\n",
" health_points -= 2\n",
" print(f\"You've lost 2 health points: {health_points} remaining.\")\n",
" if health_points <= 0:\n",
" print(\"Game Over. You lost all your health points.\")\n",
" else:\n",
" print(\"Keep walking to find the treasure!\")\n",
" \n",
" else:\n",
" print(\"Oops.. seems that's not a good choice. Try again!\")\n",
"\n",
" return "
]
},
{
Expand All @@ -143,10 +211,42 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 53,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the Haunted Mansion!\n",
"The adventure starts now! You have 10 health points and no items in your pocket.\n",
"You will navigate through the mansion choosing different options and you may encounter some surprises...!\n",
"Ohh you falled into a trap...!\n",
"You've lost 2 health points: 8 remaining.\n",
"Keep walking to find the treasure!\n",
"Congrats! You found a potion!\n",
"Now you have one item in your pocket\n",
"You encounter a ghost!\n",
"You lost the battle\n",
"You've lost 2 health points: 6 remaining.\n",
"Keep walking to find the treasure!\n",
"You encounter a ghost!\n",
"You lost the battle\n",
"You've lost 2 health points: 4 remaining.\n",
"Keep walking to find the treasure!\n",
"Congrats! You found a potion!\n",
"Now you have one item in your pocket\n",
"Ohh you falled into a trap...!\n",
"You've lost 2 health points: 2 remaining.\n",
"Keep walking to find the treasure!\n",
"You encounter a ghost!\n",
"You lost the battle\n",
"You've lost 2 health points: 0 remaining.\n",
"Game Over. You lost all your health points.\n"
]
}
],
"source": [
"run_mansion()"
]
Expand All @@ -162,7 +262,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +276,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down