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
82 changes: 74 additions & 8 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "499552c8-9e30-46e1-a706-4ac5dc64670e",
"metadata": {},
"outputs": [],
Expand All @@ -93,12 +93,22 @@
" \"\"\"\n",
" print(\"You encounter a ghost!\")\n",
" \n",
" # your code goes here"
" # your code goes here\n",
" import random\n",
" \n",
" battle_outcome = random.randint(1, 10)\n",
" \n",
" if battle_outcome <= 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": 9,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -130,7 +140,42 @@
"\n",
" \"\"\"\n",
" \n",
" # your code goes here"
" # your code goes here\n",
" import random\n",
" health_points = 10\n",
" adventurer_items = []\n",
"\n",
" while health_points > 0 and \"key\" not in adventurer_items:\n",
" choice = input(\"Do you want to go left or right? \").lower()\n",
" if choice == \"left\":\n",
" print(\"You chose to go left.\")\n",
" print(\"A random event is happening...\")\n",
" event = random.randint(1, 2)\n",
" if event == 1:\n",
" print(\"You found a potion!\")\n",
" adventurer_items.append(\"potion\")\n",
" else:\n",
" health_points -= 2\n",
" print(\"You fell into a trap and lost 2 health points.\")\n",
" print(f\"Your current health points: {health_points}\")\n",
" print(\"You need to find the key to unlock the door.\")\n",
" elif choice == \"right\":\n",
" print(\"You chose to go right.\")\n",
" if encounter_ghost() == True:\n",
" print(\"You found a key!\")\n",
" adventurer_items.append(\"key\")\n",
" else:\n",
" health_points -= 2\n",
" print(\"You lost 2 health points.\")\n",
" print(f\"Your current health points: {health_points}\")\n",
" else:\n",
" print(\"Invalid choice, please try again.\")\n",
"\n",
" if health_points <= 0:\n",
" print(\"Game over, you lost all your health points.\")\n",
" elif \"key\" in adventurer_items:\n",
" print(\"You unlocked the door and found the Treasure! Congratulations!\")\n",
" \n"
]
},
{
Expand All @@ -143,10 +188,31 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the Haunted Mansion!\n",
"You chose to go left.\n",
"A random event is happening...\n",
"You found a potion!\n",
"You chose to go right.\n",
"You encounter a ghost!\n",
"You lost the battle...\n",
"You lost 2 health points.\n",
"Your current health points: 8\n",
"You chose to go right.\n",
"You encounter a ghost!\n",
"You defeated the ghost!\n",
"You found a key!\n",
"You unlocked the door and found the Treasure! Congratulations!\n"
]
}
],
"source": [
"run_mansion()"
]
Expand All @@ -162,7 +228,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +242,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down