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
83 changes: 70 additions & 13 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"id": "a7ab83fd",
"metadata": {},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "499552c8-9e30-46e1-a706-4ac5dc64670e",
"metadata": {},
"outputs": [],
Expand All @@ -92,22 +102,26 @@
" and return something that indicates the ghost defeated the adventurer.\n",
" \"\"\"\n",
" print(\"You encounter a ghost!\")\n",
" \n",
" # your code goes here"
"\n",
" result = random.randrange(0, 10)\n",
" if result <= 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": 20,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# main function for the game\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",
Expand All @@ -129,8 +143,39 @@
" 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",
" print(\"Welcome to the Haunted Mansion!\")\n",
" health = 10\n",
" items = []\n",
"\n",
" while health > 0 or \"key\" in items:\n",
" print(f\"Your health {health}\")\n",
" direction = input(\"Choose left or right path: \").lower()\n",
"\n",
" if direction == \"left\":\n",
" if random.choice([0,1]): # 50/50 chance that found a potion\n",
" items.append(\"potion\")\n",
" else: # IT IS A TRAP!\n",
" health -= 2\n",
" elif direction == \"right\":\n",
" if encounter_ghost():\n",
" items.append(\"key\")\n",
" else:\n",
" health -= 2\n",
" else:\n",
" print(\"Wrong input. Write left or write right\")\n",
"\n",
" if health == 0:\n",
" print(\"Game over, you lost all your health points.\")\n",
" return\n",
" \n",
" if \"key\" in items:\n",
" print(\"You unlocked the door and found the Treasure! Congratulations!\")\n",
" return\n",
"\n",
" print(\"No key found yet! Go on!\")\n",
" \n",
" "
]
},
{
Expand All @@ -143,10 +188,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 21,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the Haunted Mansion!\n",
"Your health 10\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 +219,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "ironhack_prework",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +233,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.11.14"
}
},
"nbformat": 4,
Expand Down