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
137 changes: 128 additions & 9 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "3732d2a1",
"metadata": {},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "499552c8-9e30-46e1-a706-4ac5dc64670e",
"metadata": {},
"outputs": [],
Expand All @@ -92,13 +102,43 @@
" and return something that indicates the ghost defeated the adventurer.\n",
" \"\"\"\n",
" print(\"You encounter a ghost!\")\n",
" \n",
" # your code goes here"
"\n",
" # your code goes here\n",
" # random number between 1 and 10 \n",
" num = random.randint(1, 10)\n",
" if num <= 5:\n",
" print(\"You defeated the ghost!\")\n",
" print(\"Continue adventure\")\n",
" return True\n",
" else:\n",
" print(\"You lost the battle...\")\n",
" print(\"Game Over..\")\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "2a500fb9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You encounter a ghost!\n",
"You defeated the ghost!\n",
"Continue adventure\n"
]
}
],
"source": [
"encounter_ghost()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -130,7 +170,61 @@
"\n",
" \"\"\"\n",
" \n",
" # your code goes here"
" # your code goes here\n",
"\n",
" health = 10 \n",
" items = []\n",
" def choose_direction():\n",
" print(\"Please choose your way. Enter 1 for left, 2 for right.\")\n",
" while True:\n",
" try:\n",
" choice = int(input(\"1 = Left, 2 = Right:\"))\n",
"\n",
" if choice == 1:\n",
" return \"Left\"\n",
" elif choice == 2:\n",
" return \"Right\"\n",
" else:\n",
" print(\"Invalid input. Please enter 1 or 2.\")\n",
" except ValueError:\n",
" print(\"Numbers only.\")\n",
" \n",
" # Game Loop\n",
" while True: \n",
" print(\"\\nCurrent health:\", health)\n",
" print(\"Items:\", items)\n",
"\n",
" direction = choose_direction()\n",
" # Left path\n",
" if direction == \"Left\":\n",
" event = random.randint(1, 2)\n",
"\n",
" if event == 1:\n",
" print(\"You have found a potion. Potion saved to items.\")\n",
" items.append(\"potion\")\n",
" else:\n",
" print(\"You fell into a trap.\")\n",
" health -= 2\n",
" print(\"Health is deteriorating, your health is now:\", health)\n",
" # Right path\n",
" elif direction == \"Right\":\n",
" result = encounter_ghost()\n",
"\n",
" if result:\n",
" print(\"You found a key!\")\n",
" items.append(\"key\")\n",
" else:\n",
" health -= 2\n",
" print(\"Your health is deteriorating, health is now:\", health)\n",
"\n",
" # Lose\n",
" if health <= 0:\n",
" print(\"Game over, you have lost all your health.\")\n",
" break\n",
" # Win\n",
" if \"key\" in items:\n",
" print(\"You unlocked the door and found the Treasure. You won the game!\")\n",
" break\n"
]
},
{
Expand All @@ -143,10 +237,35 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the Haunted Mansion!\n",
"\n",
"Current health: 10\n",
"Items: []\n",
"Please choose your way. Enter 1 for left, 2 for right.\n",
"You encounter a ghost!\n",
"You lost the battle...\n",
"Game Over..\n",
"Your health is deteriorating, health is now: 8\n",
"\n",
"Current health: 8\n",
"Items: []\n",
"Please choose your way. Enter 1 for left, 2 for right.\n",
"You encounter a ghost!\n",
"You defeated the ghost!\n",
"Continue adventure\n",
"You found a key!\n",
"You unlocked the door and found the Treasure. You won the game!\n"
]
}
],
"source": [
"run_mansion()"
]
Expand All @@ -162,7 +281,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +295,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down