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
86 changes: 80 additions & 6 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@
" Good luck!"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "119ba7a3",
"metadata": {},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -93,7 +103,15 @@
" \"\"\"\n",
" print(\"You encounter a ghost!\")\n",
" \n",
" # your code goes here"
" # your code goes here\n",
" event = random.randint(1,10)\n",
" if event <= 5:\n",
" print(\"You defeated the ghost!\")\n",
" return True\n",
" else:\n",
" print(\"You lost the battle...\")\n",
" return False\n",
" "
]
},
{
Expand Down Expand Up @@ -130,7 +148,49 @@
"\n",
" \"\"\"\n",
" \n",
" # your code goes here"
" # your code goes here\n",
" health = 10 \n",
" items_adventurer = []\n",
"\n",
" while health > 0:\n",
" choice = input(\"Choose between two paths: (left or right)\").lower()\n",
" \n",
" if choice == 'left':\n",
" event1 = random.choice([\"potion\", \"trap\"])\n",
"\n",
" if event1 == 'potion':\n",
" print(\"You found a potion!\")\n",
" items_adventurer.append('potion')\n",
" else:\n",
" print(\"You feel into a trap!, You lose 2 points of health)\")\n",
" health -=2\n",
"\n",
" elif choice == 'right':\n",
" event2 = encounter_ghost()\n",
"\n",
" if event2:\n",
" print(\"You defeated the ghost and found a key!\")\n",
" items_adventurer.append(\"key\")\n",
" else:\n",
" print(\"The ghost won!, You lose 2 points of health =()\")\n",
" health -= 2\n",
"\n",
" else:\n",
" print(\"Invalid choice. Try again\")\n",
" continue\n",
"\n",
" #Health\n",
" if health <= 0:\n",
" print(\"Game over, you lost all your health points.\")\n",
" return \n",
" \n",
" #The player has the key\n",
" if \"key\" in items_adventurer:\n",
" print(\"You unlocked the door and found the Treasure! Congratulations!\")\n",
" return \n",
" else:\n",
" print(\"You don't have the key. Try again from the beginning\")\n",
"\n"
]
},
{
Expand All @@ -143,10 +203,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the Haunted Mansion!\n"
]
}
],
"source": [
"run_mansion()"
]
Expand All @@ -158,11 +226,17 @@
"source": [
"This should print the game's narrative and prompt the user to make choices and fight ghosts. The game ends when the adventurer finds the key or loses all their health points. "
]
},
{
"cell_type": "markdown",
"id": "96e0af2d",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +250,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.10.2"
}
},
"nbformat": 4,
Expand Down