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
156 changes: 115 additions & 41 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,40 @@
]
},
{
"cell_type": "markdown",
"id": "8846d61d-cf9d-4ad4-bbb8-1ecb3bb22005",
"cell_type": "code",
"execution_count": 1,
"id": "6531a5b4",
"metadata": {},
"outputs": [],
"source": [
"*Introduction to Functions*:\n",
"\n",
" Functions are blocks of code that perform a specific task. They are used to break up complex code into smaller, more manageable parts, which can make your code easier to read and understand. Functions can also be reused multiple times throughout your code, which can save you a lot of time and effort.\n",
"\n",
" Functions are defined using the def keyword, followed by the name of the function and a set of parentheses. Inside the parentheses, you can list any arguments that the function needs in order to perform its task. These arguments are optional, but they can be useful if you need to pass data into the function from outside.\n",
"\n",
" Once you have defined a function, you can call it from anywhere in your code using its name and passing any necessary arguments. When the function is called, the code inside it is executed, and any values that it returns are passed back to the calling code.\n",
"\n",
" In this exercise, we have defined a function called encounter_ghost that simulates a battle between the adventurer and a ghost, and run_mansion. Your task is to complete these functions by using flow control statements such as if, else, while, for loops, and nested loops. Remember to pay attention to the instructions and comments provided in the function to help guide you. Once you have completed the function encounter_ghost, you can call it from the main code to simulate the battle and test your implementation.\n",
"\n",
" Good luck!"
"import random"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "499552c8-9e30-46e1-a706-4ac5dc64670e",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You encounter a ghost!\n",
"You lost the battle!\n"
]
},
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def encounter_ghost():\n",
" \"\"\"\n",
Expand All @@ -91,14 +102,27 @@
" 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",
" '''\n",
" prompt the user to choose a path to take at each intersection. Each path should have its unique challenges and obstacles that the adventurer must overcome.\n",
"\n",
" '''\n",
" num = random.randint(1,10)\n",
" \n",
" print(\"You encounter a ghost!\")\n",
" \n",
" # your code goes here"
" if num <= 5:\n",
" print(\"You defeated the ghost!\")\n",
" return True\n",
" else:\n",
" print(\"You lost the battle!\")\n",
" return False\n",
" \n",
"encounter_ghost()"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"metadata": {},
"outputs": [],
Expand All @@ -108,29 +132,41 @@
" \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",
"\n",
" If they choose \"left\", a random event occurs. There is a 50% chance that the adventurer will find a potion and a 50% chance that they will \n",
" fall into a trap and lose 2 health points. If they find the potion, it is saved into the adventurer's items. \n",
" If they fall into a trap, 2 points are taken out of the adventurer's health points.\n",
"\n",
" If they choose \"right\", the \"encounter_ghost()\" function is called to handle the battle between the adventurer and the ghost. \n",
" If the adventurer wins, they find a key which is saved into the adventurer's items. If they lose, they lose 2 health points.\n",
" Hint: remember to check what encounter_ghost() returned to make know if they won or lost.\n",
"\n",
" If the adventurer chooses something other than \"left\" or \"right\", they are prompted to try again.\n",
" points = 10\n",
" items = []\n",
" \n",
" while points > 0 or 'key' not in items:\n",
"\n",
" If the adventurer's health points reach 0 or less, the message \"Game over, you lost all your health points.\" is printed.\n",
" choose_path = input(\"Please choose path, left or right?\")\n",
"\n",
" If the adventurer has the key, they can unlock the door and find the Treasure. This message is printed \"You unlocked the door and found the \n",
" Treasure! Congratulations!\".\n",
" If they don't have the key, they are prompted to find it from the beginning.\n",
" if choose_path.lower() not in ['left','right']:\n",
" print('Please choose between left and right!')\n",
"\n",
" \"\"\"\n",
" \n",
" # your code goes here"
" if choose_path.lower() =='left':\n",
" ran = random.randint(1,2)\n",
" print(f\"random number is {ran}\")\n",
" if ran == 1:\n",
" items.append('potion')\n",
" print(\"You received a potion!\")\n",
" else:\n",
" print('You fell into a trap, 2 points will be taken from your points pocket!')\n",
" points -= 2\n",
" print(f\"Current point is {points}\")\n",
" \n",
" if choose_path.lower() =='right':\n",
" if encounter_ghost():\n",
" items.append('key')\n",
" print(\"You received a key!\")\n",
" print(\"You unlocked the door and found the Treasure! Congratulations!\")\n",
" break \n",
" else:\n",
" points -= 2\n",
" print(f\"Current point is {points}\")\n",
" \n",
" if points <= 0: \n",
" print(\"Game over, you lost all your health points.\")\n",
" break\n",
"\n"
]
},
{
Expand All @@ -143,10 +179,48 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the Haunted Mansion!\n",
"random number is 2\n",
"You fell into a trap, 2 points will be taken from your points pocket!\n",
"Current point is 8\n",
"random number is 1\n",
"You received a potion!\n",
"random number is 1\n",
"You received a potion!\n",
"random number is 1\n",
"You received a potion!\n",
"random number is 1\n",
"You received a potion!\n",
"random number is 2\n",
"You fell into a trap, 2 points will be taken from your points pocket!\n",
"Current point is 6\n",
"random number is 1\n",
"You received a potion!\n",
"random number is 1\n",
"You received a potion!\n",
"random number is 1\n",
"You received a potion!\n",
"random number is 2\n",
"You fell into a trap, 2 points will be taken from your points pocket!\n",
"Current point is 4\n",
"random number is 2\n",
"You fell into a trap, 2 points will be taken from your points pocket!\n",
"Current point is 2\n",
"random number is 2\n",
"You fell into a trap, 2 points will be taken from your points pocket!\n",
"Current point is 0\n",
"Game over, you lost all your health points.\n"
]
}
],
"source": [
"run_mansion()"
]
Expand All @@ -162,7 +236,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "py310",
"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.18"
}
},
"nbformat": 4,
Expand Down