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
105 changes: 97 additions & 8 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,26 @@
"execution_count": null,
"id": "499552c8-9e30-46e1-a706-4ac5dc64670e",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You encounter a ghost!\n",
"You lost the battle...\n"
]
},
{
"data": {
"text/plain": [
"'the ghost defeated the adventurer'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def encounter_ghost():\n",
" \"\"\"\n",
Expand All @@ -93,15 +112,36 @@
" \"\"\"\n",
" print(\"You encounter a ghost!\")\n",
" \n",
" # your code goes here"
" # your code goes here\n",
" random_number = int(input(\"Introduce a number between 1 and 10:\"))\n",
"\n",
" if random_number <=5:\n",
" print (\"You defeated the ghost!\")\n",
" return \"the adventurer defeated the ghost\"\n",
" else:\n",
" print (\"You lost the battle...\")\n",
" return \"the ghost defeated the adventurer\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the Haunted Mansion!\n",
"Try again from the beginning.\n",
"Invalid path, try again!\n",
"Try again from the beginning.\n",
"Invalid path, try again!\n",
"Try again from the beginning.\n"
]
}
],
"source": [
"# main function for the game\n",
"def run_mansion():\n",
Expand Down Expand Up @@ -130,7 +170,44 @@
"\n",
" \"\"\"\n",
" \n",
" # your code goes here"
" # your code goes here\n",
" health_points = 10\n",
" items = []\n",
"\n",
" while health_points > 0:\n",
"\n",
" choice = input(\"left or right?\")\n",
"\n",
" if choice == \"left\":\n",
" event = input (\"potion or trap? \")\n",
"\n",
" if event == \"potion\":\n",
" items.append(\"potion\")\n",
" else:\n",
" health_points -=2 \n",
"\n",
" elif choice == \"right\":\n",
" result = encounter_ghost()\n",
"\n",
" if result == \"the adventurer defeated the ghost\":\n",
" items.append(\"key\")\n",
" else:\n",
" health_points -= 2\n",
" \n",
" else:\n",
" print (\"Invalid path, try again!\") \n",
"\n",
" if health_points <= 0:\n",
" print (\"Game over, you lost all your health points.\")\n",
" break\n",
"\n",
" if \"key\" in items:\n",
" print (\"You unlocked the door and found the Treasure! Congratulations!\")\n",
" break\n",
" else:\n",
" print (\"Try again from the beginning.\")\n",
"\n",
"run_mansion()"
]
},
{
Expand All @@ -143,10 +220,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "NameError",
"evalue": "name 'run_mansion' is not defined",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mNameError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[1]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m run_mansion()\n",
"\u001b[31mNameError\u001b[39m: name 'run_mansion' is not defined"
]
}
],
"source": [
"run_mansion()"
]
Expand All @@ -162,7 +251,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +265,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.3"
}
},
"nbformat": 4,
Expand Down