diff --git a/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb new file mode 100644 index 0000000..ce36a57 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -0,0 +1,324 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f", + "metadata": { + "tags": [] + }, + "source": [ + "# Lab | Flow Control" + ] + }, + { + "cell_type": "markdown", + "id": "303cceb0-f898-4c5f-99bd-c04e131e1ded", + "metadata": {}, + "source": [ + "Objective: Practice how to use programming constructs like if/else statements and loops to control the flow of a program's execution." + ] + }, + { + "cell_type": "markdown", + "id": "42fd260a-9eb2-4c39-9989-f2c8c70552ec", + "metadata": {}, + "source": [ + "## Challenge: The Haunted Mansion" + ] + }, + { + "cell_type": "markdown", + "id": "7086f8f7-ded2-48e5-b966-7ec44fb57ca5", + "metadata": {}, + "source": [ + "You are a brave adventurer who has decided to explore the Haunted Mansion, a decrepit old building that is rumored to be haunted by ghosts and spirits. Your objective is to find the treasure that is hidden somewhere in the mansion.\n", + "\n", + "## Requirements\n", + "\n", + "- Your script should have at least two functions: \"run_mansion()\" and \"encounter_ghost()\".\n", + "- Your script should use if-else statements, while loops, for loops, combined loops, or nested loops to control the flow of execution.\n", + "- Your script should prompt the user for input to make decisions on which path to take or what actions to perform.\n", + "- Your script should include random events and obstacles that can either help or hinder the adventurer in their quest.\n", + "- Your script should have an objective of finding the treasure at the end of the mansion.\n", + "\n", + "## Instructions\n", + "\n", + "- Begin by creating a list of items that the adventurer can pick up along the way. These items will be used to help the adventurer overcome obstacles and defeat ghosts. Examples of items can be weapons, potions, keys, etc.\n", + "\n", + "- Complete the function called \"run_mansion()\" that serves as the main function for the game. Within \"run_mansion()\", 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", + "- Use loops to check if the adventurer has enough health points to continue the game. If their health points drop to zero, the game is over.\n", + "\n", + "- Complete the function called \"encounter_ghost()\" that will be used to handle ghost encounters. The function should use random events to determine the outcome of the encounter, and the adventurer should use their items to help them defeat the ghost.\n", + "\n", + "- Use loops to generate random events or items along the way. These events can either help or hinder the adventurer, and the outcome should be based on random chance.\n", + "\n", + "- At the end of the mansion, the adventurer will find the treasure, and the game will end.\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "8846d61d-cf9d-4ad4-bbb8-1ecb3bb22005", + "metadata": {}, + "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!" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "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", + " \n", + " import random\n", + "\n", + "def encounter_ghost(items):\n", + " \"\"\"\n", + " Simulates an encounter with a ghost.\n", + " Uses random chance and optionally items to determine outcome.\n", + " \"\"\"\n", + " print(\"\\n๐Ÿ‘ป You encounter a ghost!\")\n", + " result = random.randint(1, 10)\n", + "\n", + " if result <= 5:\n", + " print(\"๐Ÿ’ฅ You defeated the ghost!\")\n", + " return True\n", + " else:\n", + " if \"magic potion\" in items:\n", + " print(\"๐Ÿงช You used a magic potion to escape the ghost!\")\n", + " items.remove(\"magic potion\")\n", + " return True\n", + " else:\n", + " print(\"โ˜ ๏ธ You lost the battle...\")\n", + " return False\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", + "metadata": {}, + "outputs": [ + { + "ename": "IndentationError", + "evalue": "expected an indented block after function definition on line 27 (1283088849.py, line 28)", + "output_type": "error", + "traceback": [ + " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[6]\u001b[39m\u001b[32m, line 28\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mhealth = 3\u001b[39m\n ^\n\u001b[31mIndentationError\u001b[39m\u001b[31m:\u001b[39m expected an indented block after function definition on line 27\n" + ] + } + ], + "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", + "\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", + "\n", + " If the adventurer's health points reach 0 or less, the message \"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", + " Treasure! Congratulations!\".\n", + " If they don't have the key, they are prompted to find it from the beginning.\n", + "\n", + " \"\"\"\n", + " def run_mansion():\n", + " health = 3\n", + " items = []\n", + " paths = [\"left\", \"right\", \"center\"]\n", + "\n", + " print(\"๐Ÿฐ Welcome to the Haunted Mansion!\")\n", + " print(\"Your goal is to find the hidden treasure without losing all your health.\")\n", + "\n", + " while health > 0:\n", + " path = input(\"\\nWhich path will you take? (left / right / center): \").lower().strip()\n", + "\n", + " if path not in paths:\n", + " print(\"๐Ÿšซ Invalid path. Try again.\")\n", + " continue\n", + "\n", + " event = random.choice([\"item\", \"ghost\", \"nothing\"])\n", + "\n", + " if event == \"item\":\n", + " item = random.choice([\"sword\", \"magic potion\", \"key\"])\n", + " items.append(item)\n", + " print(f\"๐ŸŽ You found a {item}!\")\n", + "\n", + " elif event == \"ghost\":\n", + " survived = encounter_ghost(items)\n", + " if not survived:\n", + " health -= 1\n", + " print(f\"โค๏ธ Health reduced to {health}\")\n", + "\n", + " else:\n", + " print(\"๐ŸŒซ๏ธ Nothing happened... The room is empty.\")\n", + "\n", + " if random.randint(1, 8) == 5:\n", + " print(\"\\n๐Ÿ’ฐ You found the treasure! You win!\")\n", + " break\n", + "\n", + " if health == 0:\n", + " print(\"\\n๐Ÿ’€ You lost all your health. Game Over.\")\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "f71bba3c-2a4e-4660-8bc4-73a12584050b", + "metadata": {}, + "outputs": [], + "source": [ + "def run_mansion():\n", + " import random\n", + "\n", + " health = 3\n", + " items = []\n", + " paths = [\"left\", \"right\", \"center\"]\n", + "\n", + " print(\"๐Ÿฐ Welcome to the Haunted Mansion!\")\n", + " print(\"Your goal is to find the hidden treasure without losing all your health.\")\n", + "\n", + " while health > 0:\n", + " path = input(\"\\nWhich path will you take? (left / right / center): \").lower().strip()\n", + "\n", + " if path not in paths:\n", + " print(\"๐Ÿšซ Invalid path. Try again.\")\n", + " continue\n", + "\n", + " event = random.choice([\"item\", \"ghost\", \"nothing\"])\n", + "\n", + " if event == \"item\":\n", + " item = random.choice([\"sword\", \"magic potion\", \"key\"])\n", + " items.append(item)\n", + " print(f\"๐ŸŽ You found a {item}!\")\n", + "\n", + " elif event == \"ghost\":\n", + " survived = encounter_ghost(items)\n", + " if not survived:\n", + " health -= 1\n", + " print(f\"โค๏ธ Health reduced to {health}\")\n", + "\n", + " else:\n", + " print(\"๐ŸŒซ๏ธ Nothing happened... The room is empty.\")\n", + "\n", + " if random.randint(1, 8) == 5:\n", + " print(\"\\n๐Ÿ’ฐ You found the treasure! You win!\")\n", + " break\n", + "\n", + " if health == 0:\n", + " print(\"\\n๐Ÿ’€ You lost all your health. Game Over.\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "9e13a33c-38e5-44b3-bd1b-9a642c962c89", + "metadata": {}, + "source": [ + "To run the game, simply call the run_mansion() function:" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐Ÿฐ Welcome to the Haunted Mansion!\n", + "Your goal is to find the hidden treasure without losing all your health.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "\n", + "Which path will you take? (left / right / center): left\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐ŸŒซ๏ธ Nothing happened... The room is empty.\n", + "\n", + "๐Ÿ’ฐ You found the treasure! You win!\n" + ] + } + ], + "source": [ + "run_mansion()" + ] + }, + { + "cell_type": "markdown", + "id": "88212f63-3bdb-479f-bf6c-4ecd0685d39a", + "metadata": {}, + "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. " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..ce36a57 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -77,7 +77,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "499552c8-9e30-46e1-a706-4ac5dc64670e", "metadata": {}, "outputs": [], @@ -93,15 +93,44 @@ " \"\"\"\n", " print(\"You encounter a ghost!\")\n", " \n", - " # your code goes here" + " import random\n", + "\n", + "def encounter_ghost(items):\n", + " \"\"\"\n", + " Simulates an encounter with a ghost.\n", + " Uses random chance and optionally items to determine outcome.\n", + " \"\"\"\n", + " print(\"\\n๐Ÿ‘ป You encounter a ghost!\")\n", + " result = random.randint(1, 10)\n", + "\n", + " if result <= 5:\n", + " print(\"๐Ÿ’ฅ You defeated the ghost!\")\n", + " return True\n", + " else:\n", + " if \"magic potion\" in items:\n", + " print(\"๐Ÿงช You used a magic potion to escape the ghost!\")\n", + " items.remove(\"magic potion\")\n", + " return True\n", + " else:\n", + " print(\"โ˜ ๏ธ You lost the battle...\")\n", + " return False\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "IndentationError", + "evalue": "expected an indented block after function definition on line 27 (1283088849.py, line 28)", + "output_type": "error", + "traceback": [ + " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[6]\u001b[39m\u001b[32m, line 28\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mhealth = 3\u001b[39m\n ^\n\u001b[31mIndentationError\u001b[39m\u001b[31m:\u001b[39m expected an indented block after function definition on line 27\n" + ] + } + ], "source": [ "# main function for the game\n", "def run_mansion():\n", @@ -129,8 +158,93 @@ " If they don't have the key, they are prompted to find it from the beginning.\n", "\n", " \"\"\"\n", - " \n", - " # your code goes here" + " def run_mansion():\n", + " health = 3\n", + " items = []\n", + " paths = [\"left\", \"right\", \"center\"]\n", + "\n", + " print(\"๐Ÿฐ Welcome to the Haunted Mansion!\")\n", + " print(\"Your goal is to find the hidden treasure without losing all your health.\")\n", + "\n", + " while health > 0:\n", + " path = input(\"\\nWhich path will you take? (left / right / center): \").lower().strip()\n", + "\n", + " if path not in paths:\n", + " print(\"๐Ÿšซ Invalid path. Try again.\")\n", + " continue\n", + "\n", + " event = random.choice([\"item\", \"ghost\", \"nothing\"])\n", + "\n", + " if event == \"item\":\n", + " item = random.choice([\"sword\", \"magic potion\", \"key\"])\n", + " items.append(item)\n", + " print(f\"๐ŸŽ You found a {item}!\")\n", + "\n", + " elif event == \"ghost\":\n", + " survived = encounter_ghost(items)\n", + " if not survived:\n", + " health -= 1\n", + " print(f\"โค๏ธ Health reduced to {health}\")\n", + "\n", + " else:\n", + " print(\"๐ŸŒซ๏ธ Nothing happened... The room is empty.\")\n", + "\n", + " if random.randint(1, 8) == 5:\n", + " print(\"\\n๐Ÿ’ฐ You found the treasure! You win!\")\n", + " break\n", + "\n", + " if health == 0:\n", + " print(\"\\n๐Ÿ’€ You lost all your health. Game Over.\")\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "f71bba3c-2a4e-4660-8bc4-73a12584050b", + "metadata": {}, + "outputs": [], + "source": [ + "def run_mansion():\n", + " import random\n", + "\n", + " health = 3\n", + " items = []\n", + " paths = [\"left\", \"right\", \"center\"]\n", + "\n", + " print(\"๐Ÿฐ Welcome to the Haunted Mansion!\")\n", + " print(\"Your goal is to find the hidden treasure without losing all your health.\")\n", + "\n", + " while health > 0:\n", + " path = input(\"\\nWhich path will you take? (left / right / center): \").lower().strip()\n", + "\n", + " if path not in paths:\n", + " print(\"๐Ÿšซ Invalid path. Try again.\")\n", + " continue\n", + "\n", + " event = random.choice([\"item\", \"ghost\", \"nothing\"])\n", + "\n", + " if event == \"item\":\n", + " item = random.choice([\"sword\", \"magic potion\", \"key\"])\n", + " items.append(item)\n", + " print(f\"๐ŸŽ You found a {item}!\")\n", + "\n", + " elif event == \"ghost\":\n", + " survived = encounter_ghost(items)\n", + " if not survived:\n", + " health -= 1\n", + " print(f\"โค๏ธ Health reduced to {health}\")\n", + "\n", + " else:\n", + " print(\"๐ŸŒซ๏ธ Nothing happened... The room is empty.\")\n", + "\n", + " if random.randint(1, 8) == 5:\n", + " print(\"\\n๐Ÿ’ฐ You found the treasure! You win!\")\n", + " break\n", + "\n", + " if health == 0:\n", + " print(\"\\n๐Ÿ’€ You lost all your health. Game Over.\")\n" ] }, { @@ -143,10 +257,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐Ÿฐ Welcome to the Haunted Mansion!\n", + "Your goal is to find the hidden treasure without losing all your health.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "\n", + "Which path will you take? (left / right / center): left\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐ŸŒซ๏ธ Nothing happened... The room is empty.\n", + "\n", + "๐Ÿ’ฐ You found the treasure! You win!\n" + ] + } + ], "source": [ "run_mansion()" ] @@ -176,7 +316,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.2" } }, "nbformat": 4,