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
172 changes: 153 additions & 19 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,60 +77,165 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "69d5ff55",
"metadata": {},
"outputs": [],
"source": [
"list_of_items = [\"knife\", \"potion\", \"keys\"]"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "499552c8-9e30-46e1-a706-4ac5dc64670e",
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"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",
" If the random number is less than or equal to 5, the adventurer defeats the ghost. \n",
" 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",
" If the random number is greater than 5, the adventurer loses the battle. \n",
" 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",
" print(\n",
" f'''You encounter a ghost!\n",
" It is determined to kill you! Fight for your life!\n",
" As ridiculous as it sounds: there is 50% chance of defeating the ghost.\n",
" The power of the mind is just incredible: the die is cast!'''\n",
" )\n",
" random_user_input = input(\n",
" f'Write what your character should say in this situation: '\n",
" )\n",
" print(\n",
" f\"\"\"The adverturer said aloud: '{random_user_input}!'\n",
" And the darkness was around him. And the sound of the wind was subtle.\"\"\"\n",
" )\n",
" random_user_input = input(\n",
" f'Press to continue. This is just to make you wait a little bit.'\n",
" )\n",
" if random.random() <= 5:\n",
" print(\n",
" f'''The adventurer fought with his soul and his heart. \n",
" The ghost is defeated; the light has won once more!\n",
" you defeated the ghost!'''\n",
" )\n",
" return True\n",
" else:\n",
" print(\n",
" f'You lost the batlle. Night has won!'\n",
" )\n",
" return False\n",
" \n",
" # your code goes here"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 41,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"# main function for the game\n",
"\n",
"def run_mansion():\n",
" \n",
" print(\"Welcome to the Haunted Mansion!\")\n",
" \n",
"\n",
" \"\"\"\n",
" Simulates an adventure through a haunted mansion. The adventurer starts with 10 health points and no items.\n",
" Simulates an adventure through a haunted mansion. The adventurer starts with 10 health points \n",
" 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 choose \"left\", a random event occurs. There is a 50% chance that the adventurer \n",
" will find a potion and a 50% chance that they will fall into a trap and lose 2 health points. \n",
" 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",
" If they choose \"right\", the \"encounter_ghost()\" function is called to handle the battle between \n",
" the adventurer and the ghost. \n",
" If the adventurer wins, they find a key which is saved into the adventurer's items. \n",
" 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",
"\n",
" If the adventurer's health points reach 0 or less, the message \"Game over, you lost all your health points.\" is printed.\n",
" If the adventurer's health points reach 0 or less, the message \n",
" \"Game over, you lost all your health points.\" is printed.\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",
" If the adventurer has the key, they can unlock the door and find the Treasure. \n",
" 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",
"\n",
" \"\"\"\n",
" \n",
" # your code goes here"
"\n",
" # your code goes here\n",
" items = []\n",
" lives = 10\n",
"\n",
" while lives > 0:\n",
" choice1 = input(\n",
" f'You are in the darkest night, between two paths in the mansion, choose for your life\\n'\n",
" f'right or left (r/l), you need to find a key in this laberinth '\n",
" )\n",
" if choice1 == \"l\":\n",
" if random.random() >= 0.5:\n",
" items.append(\"potion\")\n",
" print(\n",
" f'You found a potion. It is now stored in your items!'\n",
" )\n",
" else:\n",
" lives = lives - 2\n",
" print(\n",
" f'''A ghost has entered the room. It went through you!\n",
" It was a trap. \n",
" You are not bleeding but your life is in danger. Lives left: {lives}.'''\n",
" )\n",
" elif choice1 == \"r\":\n",
" encounter = encounter_ghost()\n",
" if encounter == True:\n",
" items.append(\"key\")\n",
" print(\n",
" f'You found a mysterious key on the floor. It is now saved into your items.'\n",
" )\n",
" show_items = input(f'Do you want to see your items? (y/n): ')\n",
" if show_items == \"y\":\n",
" for item in items:\n",
" print(\n",
" f'You have one {item}.'\n",
" )\n",
" choice2 = input(\"Do you want to use it to unlock a door? (y/n): \")\n",
" print(\n",
" f\"The adventurer said: {choice2}\"\n",
" )\n",
" if choice2 == \"y\":\n",
" print(\n",
" \"You unlocked the door and found the Treasure! Congratulations!\"\n",
" )\n",
" else:\n",
" encounter_ghost()\n",
" elif encounter == False:\n",
" lives = lives - 2\n",
" print(\n",
" f'''You are already bleeding, the battle was hard. \n",
" Lives left: {lives}.'''\n",
" )\n",
" else:\n",
" print(\n",
" f'Try again!'\n",
" )\n",
" print(\"Game over, you lost all your health points.\")"
]
},
{
Expand All @@ -143,10 +248,39 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 42,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the Haunted Mansion!\n",
"You found a potion. It is now stored in your items!\n",
"A ghost has entered the room. It went through you!\n",
" It was a trap. \n",
" You are not bleeding but your life is in danger. Lives left: 8.\n",
"You found a potion. It is now stored in your items!\n",
"You found a potion. It is now stored in your items!\n",
"You found a potion. It is now stored in your items!\n",
"A ghost has entered the room. It went through you!\n",
" It was a trap. \n",
" You are not bleeding but your life is in danger. Lives left: 6.\n",
"A ghost has entered the room. It went through you!\n",
" It was a trap. \n",
" You are not bleeding but your life is in danger. Lives left: 4.\n",
"You found a potion. It is now stored in your items!\n",
"A ghost has entered the room. It went through you!\n",
" It was a trap. \n",
" You are not bleeding but your life is in danger. Lives left: 2.\n",
"A ghost has entered the room. It went through you!\n",
" It was a trap. \n",
" You are not bleeding but your life is in danger. Lives left: 0.\n",
"Game over, you lost all your health points.\n"
]
}
],
"source": [
"run_mansion()"
]
Expand All @@ -162,7 +296,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +310,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down