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
196 changes: 163 additions & 33 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,120 @@
" Good luck!"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "84f77936",
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"# Inventory (Cross, Shield, Holy Water, Salt)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "b0c9aab9",
"metadata": {},
"outputs": [],
"source": [
"def ghosty(inventory, health):\n",
" print(\"🌑 A Ghosty emerges! It drains your energy slowly.\")\n",
" damage = random.randint(1, 3)\n",
"\n",
" if \"Cross\" in inventory:\n",
" print(f\"You fight back with your cross! The Ghosty vanished\")\n",
" else:\n",
" return health - damage"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "e67d6e0f",
"metadata": {},
"outputs": [],
"source": [
"def poltergeist(inventory, health):\n",
" print(\"🪑 A Poltergeist threw a chair on you.\")\n",
" damage = random.randint(1, 4)\n",
"\n",
" if \"Shield\" in inventory:\n",
" print(f\"You protected yourself with the shield! The Poltergeist vanished\")\n",
" else:\n",
" return health - damage"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "ea7f4393",
"metadata": {},
"outputs": [],
"source": [
"def banshee(inventory, health):\n",
" print(\"📢 A Banshee appeared\")\n",
" damage = random.randint(2, 4)\n",
"\n",
" if \"Holy Water\" in inventory:\n",
" print(f\"You threw the Holy Water on Banshee! The Banshee vanished\")\n",
" else:\n",
" return health - damage"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "f7ed4a5e",
"metadata": {},
"outputs": [],
"source": [
"def spook(inventory, health):\n",
" print(\"👻 A Spooky appeared\")\n",
" damage = round(health * random.uniform(0, 0.5), 2)\n",
"\n",
" if \"Salt\" in inventory:\n",
" print(f\"You threw the Salt on Spooky! The Spooky vanished\")\n",
" else:\n",
" return health - damage"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "499552c8-9e30-46e1-a706-4ac5dc64670e",
"metadata": {},
"outputs": [],
"source": [
"def encounter_ghost():\n",
" \"\"\"\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",
" 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",
"def encounter_ghost(inventory, health):\n",
" print(\"You encounter a ghost!\")\n",
" \n",
" # your code goes here"
" # Create the ghosts list\n",
" ghost_types = [\"Banshee\", \"Ghosty\", \"Poltergeist\", \"Spooky\"]\n",
" # pick a random ghost\n",
" pick_ghost = random.choice(ghost_types)\n",
"\n",
" # Create the list of items in the bad\n",
" item = [\"Cross\", \"Shield\", \"Holy Water\", \"Salt\"]\n",
" # pick a random item\n",
" pick_item = random.choice(item)\n",
" inventory.append(pick_item)\n",
"\n",
" if pick_ghost == \"Banshee\":\n",
" health = banshee(inventory, health)\n",
" print(f\"you found the item {pick_item}\")\n",
" elif pick_ghost == \"Poltergeist\":\n",
" health = poltergeist(inventory, health)\n",
" print(f\"you found the item {pick_item}\")\n",
" elif pick_ghost == \"Ghosty\":\n",
" health = ghosty(inventory, health)\n",
" print(f\"you found the item {pick_item}\")\n",
" elif pick_ghost == \"Spooky\":\n",
" health = spook(inventory, health)\n",
" print(f\"you found the item {pick_item}\")\n",
"\n",
" return health"
]
},
{
Expand All @@ -105,31 +200,49 @@
"source": [
"# main function for the game\n",
"def run_mansion():\n",
" \n",
"\n",
" health = 10\n",
" inventory = []\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",
" 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",
" while health > 0:\n",
" # Check if the key is in the inventory\n",
" if \"Key\" in inventory:\n",
" print(\"\\n🔑 You use the Key to unlock the heavy iron door...\")\n",
" print(\"💰 You found the Treasure! Congratulations!\")\n",
" return # End the game successfully\n",
"\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",
" print(f\"\\nHP: {health}\")\n",
" choice = input(\"Choose your path: 'left' or 'right': \").lower().strip()\n",
"\n",
" If the adventurer chooses something other than \"left\" or \"right\", they are prompted to try again.\n",
" if choice == \"left\":\n",
" # 50/50 Chance logic\n",
" if random.random() < 0.5:\n",
" print(\"Empty room\")\n",
" else:\n",
" health = encounter_ghost(inventory, health)\n",
" if health <= 0:\n",
" print(\"You loose\")\n",
" else:\n",
" if random.random() < 0.5:\n",
" print(\"You found the key 🗝️\")\n",
" inventory.append(\"Key\")\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",
" elif choice == \"right\":\n",
" if random.random() > 0.5:\n",
" print(\"Empty room\")\n",
" else:\n",
" health = encounter_ghost(inventory, health)\n",
" if health <= 0:\n",
" print(\"You loose\")\n",
" else:\n",
" if random.random() < 0.5:\n",
" print(\"You found the key 🗝️\")\n",
" inventory.append(\"Key\")\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",
" Treasure! Congratulations!\".\n",
" If they don't have the key, they are prompted to find it from the beginning.\n",
" else:\n",
" print(\"The shadows whhisper...choose left or rhight door\")\n",
"\n",
" \"\"\"\n",
" \n",
" # your code goes here"
]
},
Expand All @@ -143,10 +256,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 25,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the Haunted Mansion!\n",
"\n",
"HP: 10 | Items: []\n",
"You encounter a ghost!\n",
"👻 A Spooky appeared\n",
"you found the item ['Cross']\n",
"You found the key 🗝️\n",
"\n",
"🔑 You use the Key to unlock the heavy iron door...\n",
"💰 You found the Treasure! Congratulations!\n"
]
}
],
"source": [
"run_mansion()"
]
Expand All @@ -162,7 +292,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +306,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.2"
}
},
"nbformat": 4,
Expand Down