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
163 changes: 155 additions & 8 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"metadata": {},
"outputs": [],
"source": [
"def encounter_ghost():\n",
"#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",
Expand All @@ -91,9 +91,30 @@
" 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",
" #your code goes here"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "3715dc9a-f12c-4fae-9308-69d828cdf4ee",
"metadata": {},
"outputs": [],
"source": [
"def encounter_ghost():\n",
" print(\"You encounter a ghost!\")\n",
" \n",
" # your code goes here"
" n = 0\n",
" while n < 1 or n > 10:\n",
" n = int(input(\"Choose a number between 1 and 10: \"))\n",
" if n < 1 or n > 10:\n",
" print(\"Invalid number. Please enter 1-10.\")\n",
" if n <= 5:\n",
" print(\"You defeated the ghost!\")\n",
" return \"won\"\n",
" else:\n",
" print(\"You lost the battle...\")\n",
" return \"lost\""
]
},
{
Expand Down Expand Up @@ -133,6 +154,60 @@
" # your code goes here"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "d43c2689-b38b-4d77-a4d7-771f3eaceb3a",
"metadata": {},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "c5de5d1a-d530-42a8-8dc5-7945bc148f50",
"metadata": {},
"outputs": [],
"source": [
"def run_mansion():\n",
" print(\"Welcome to the Haunted Mansion!\")\n",
" health_points = 10\n",
" items = []\n",
" while health_points>0:\n",
" print(f\"Health: {health_points} | 🎒 Items: {items}\")\n",
" choise = str(input('Go left or right: ')).lower()\n",
" if choise == 'left':\n",
" print(\"You chose to go left...\")\n",
" event = random.choice([\"potion\", \"trap\"])\n",
" if event == 'potion':\n",
" print('You won a potion!')\n",
" items.append('potion')\n",
" else:\n",
" print('This is a trap, you loose 2 points!')\n",
" health_points = health_points - 2\n",
" elif choise == 'right':\n",
" print('You choose to go right')\n",
" result = encounter_ghost()\n",
" if result == 'won':\n",
" print('Take the key!')\n",
" items.append('key')\n",
" else:\n",
" print('You lost!')\n",
" health_points = health_points - 2\n",
" else:\n",
" print(\"\\n Invalid choice. Please type 'left' or 'right'.\")\n",
" \n",
" if \"key\" in items:\n",
" print(\"\\n You unlocked the door and found the Treasure! Congratulations!\")\n",
" break\n",
" \n",
" if health_points <= 0:\n",
" print(\"\\n Game over, you lost all your health points.\")\n",
" break"
]
},
{
"cell_type": "markdown",
"id": "9e13a33c-38e5-44b3-bd1b-9a642c962c89",
Expand All @@ -143,10 +218,82 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the Haunted Mansion!\n",
"Health: 10 | 🎒 Items: []\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Go left or right: right\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"You choose to go right\n",
"You encounter a ghost!\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Choose a number between 1 and 10: 9\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"You lost the battle...\n",
"You lost!\n",
"Health: 8 | 🎒 Items: []\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Go left or right: right\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"You choose to go right\n",
"You encounter a ghost!\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Choose a number between 1 and 10: 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"You defeated the ghost!\n",
"Take the key!\n",
"\n",
" You unlocked the door and found the Treasure! Congratulations!\n"
]
}
],
"source": [
"run_mansion()"
]
Expand All @@ -162,9 +309,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -176,7 +323,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down