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
140 changes: 119 additions & 21 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,37 +77,58 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"id": "499552c8-9e30-46e1-a706-4ac5dc64670e",
"metadata": {},
"outputs": [],
"source": [
"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",
" If the random number is less than or equal to 5, the adventurer defeats the ghost. In this case, print the message \"You defeated the ghost!\" \n",
" and return something that indicates the adventurer defeated the ghost.\n",
" 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",
" for number in range (1, 10):\n",
" number = int(input(\"Enter a number\"))\n",
" if number <= 5: \n",
" print(\"You defeated the ghost\")\n",
" return \"win\"\n",
" elif number > 5:\n",
" print(\"You lost the battle\")\n",
" return \"lose\"\n",
" else:\n",
" print(\"You encounter a ghost!\")\n",
"\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",
" #If the random number is less than or equal to 5, the adventurer defeats the ghost. In this case, print the message \"You defeated the ghost!\" \n",
" #and return something that indicates the adventurer defeated the ghost.\n",
" #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",
" # print(\"You encounter a ghost!\")\n",
" \n",
" # your code goes here"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"execution_count": 17,
"id": "b2d1522c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You defeated the ghost\n"
]
}
],
"source": [
"winner = encounter_ghost()"
]
},
{
"cell_type": "markdown",
"id": "16e4ac42",
"metadata": {},
"outputs": [],
"source": [
"# main function for the game\n",
"def run_mansion():\n",
" \n",
" print(\"Welcome to the Haunted Mansion!\")\n",
" \n",
" \"\"\"\n",
" Simulates an adventure through a haunted mansion. The adventurer starts with 10 health points and no items.\n",
" Prompt the user to choose between two paths: \"left\" or \"right\". \n",
Expand All @@ -133,6 +154,83 @@
" # your code goes here"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"metadata": {},
"outputs": [],
"source": [
"# main function for the game\n",
"import random\n",
"\n",
"def run_mansion():\n",
" user_point = 10\n",
" items = set()\n",
" \n",
" print(\"Welcome to the Haunted Mansion!\")\n",
" continue_playing = \"yes\"\n",
"\n",
" while continue_playing == \"yes\" and user_point > 0:\n",
" path = input(\"Enter a path lefth or right\").lower()\n",
" if path == \"left\":\n",
" random_number = random.randint(1, 2)\n",
" guess = int(input(\"Enter a number 1 or 2\"))\n",
" if guess == random_number:\n",
" items.add(\"potion\")\n",
" print(\"You found the potion and it is saved\")\n",
" print(\"items =\", items)\n",
" else:\n",
" user_point -= 2 \n",
" print(user_point)\n",
" elif path == \"right\":\n",
" encounter_ghost()\n",
" if winner == \"You defeated the ghost\":\n",
" items.add(\"key\")\n",
" print(\"You got a key reward\")\n",
" print(\"items =\", items)\n",
" else:\n",
" user_point -= 2\n",
" print(user_point)\n",
" continue_playing = input(\"Continue playing yes/no\").lower()\n",
" return user_point, items \n",
" \n",
" \n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "7066727d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the Haunted Mansion!\n",
"You found the potion and it is saved\n",
"items = {'potion'}\n",
"You defeated the ghost\n",
"8\n"
]
},
{
"data": {
"text/plain": [
"(8, {'potion'})"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"run_mansion()\n"
]
},
{
"cell_type": "markdown",
"id": "9e13a33c-38e5-44b3-bd1b-9a642c962c89",
Expand All @@ -148,7 +246,7 @@
"metadata": {},
"outputs": [],
"source": [
"run_mansion()"
"\n"
]
},
{
Expand All @@ -162,7 +260,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +274,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down