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
110 changes: 106 additions & 4 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,26 @@
" # your code goes here"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e4909af3",
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"def encounter_ghost():\n",
" import random\n",
" print(\"you encounter a ghost!!!!\")\n",
" battle = random.randint(0,2)\n",
" if battle <= 1:\n",
" print(\"You defeated the ghost!\")\n",
" return \"Win\" \n",
" else:\n",
" print(\"you lost the battle 'encounter' \")\n",
" return \"lose\""
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -133,6 +153,65 @@
" # your code goes here"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "ca37e035",
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"def encounter_ghost():\n",
" import random\n",
" print(\"you encounter a ghost!!!!\")\n",
" battle = random.randint(0,2)\n",
" if battle <= 1:\n",
" print(\"You defeated the ghost!\")\n",
" return \"win\" \n",
" else:\n",
" print(\"you lost the battle 'encounter' \")\n",
" return \"lose\"\n",
" \n",
"\n",
"def run_mansion():\n",
" print(\"Welcome to the Haunted Mansion!\")\n",
" health = 10 \n",
" items = []\n",
"\n",
" while health > 0:\n",
" choise = input(\"where do you want to go? 'left' or 'right' \").lower()\n",
"\n",
" if choise == \"left\":\n",
" event = random.randint(1,2)\n",
" if event == 1:\n",
" print(\"you found potion\")\n",
" items.append(\"potion\")\n",
" health += 2\n",
" else:\n",
" print(\"you found a trap! ops\")\n",
" health -= 2 \n",
"\n",
" elif choise == \"right\":\n",
" result = encounter_ghost()\n",
" if result == 'win':\n",
" print(\"you found a key\")\n",
" items.append(\"key\")\n",
" else:\n",
" print(\"The ghost hurt you! You lose 2 health points\")\n",
" health -= 2\n",
" else:\n",
" print(\"Enter a vaild input! 'left' or 'right' \")\n",
" continue\n",
" print(health)\n",
"\n",
" if \"key\" in items:\n",
" print(\"congrats you found the key and won\")\n",
" break\n",
" if health <= 0:\n",
" print(\"you health is zero! you lost the game\")\n",
" break\n"
]
},
{
"cell_type": "markdown",
"id": "9e13a33c-38e5-44b3-bd1b-9a642c962c89",
Expand All @@ -143,10 +222,33 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the Haunted Mansion!\n",
"you found potion\n",
"12\n",
"you encounter a ghost!!!!\n",
"you lost the battle 'encounter' \n",
"The ghost hurt you! You lose 2 health points\n",
"10\n",
"you encounter a ghost!!!!\n",
"you lost the battle 'encounter' \n",
"The ghost hurt you! You lose 2 health points\n",
"8\n",
"you encounter a ghost!!!!\n",
"You defeated the ghost!\n",
"you found a key\n",
"8\n",
"congrats you found the key and won\n"
]
}
],
"source": [
"run_mansion()"
]
Expand All @@ -162,7 +264,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +278,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.2"
}
},
"nbformat": 4,
Expand Down