diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..86dc516 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -7,7 +7,7 @@ "tags": [] }, "source": [ - "# Lab | Flow Control" + "# Lab | Flow Control EXTRA" ] }, { @@ -15,7 +15,7 @@ "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." + "Objective: Practice how to use programming constructs like **if/else statements** and **loops** to **control the flow of a program's execution**." ] }, { @@ -23,6 +23,7 @@ "id": "42fd260a-9eb2-4c39-9989-f2c8c70552ec", "metadata": {}, "source": [ + "---\n", "## Challenge: The Haunted Mansion" ] }, @@ -33,14 +34,16 @@ "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", + "---\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", + "- 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", + "---\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", @@ -62,17 +65,24 @@ "id": "8846d61d-cf9d-4ad4-bbb8-1ecb3bb22005", "metadata": {}, "source": [ - "*Introduction to Functions*:\n", + "---\n", + "## **Introduction to Functions**:\n", + "- Functions are blocks of code that perform a specific task.\n", + "- They are used to break up complex code into smaller, more manageable parts, which can make your code easier to read and understand.\n", + "- Functions can also be reused multiple times throughout your code, which can save you a lot of time and effort.\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. \n", + "- These arguments are optional, but they can be useful if you need to pass data into the function from outside.\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\n", + "function is called, the code inside it is executed, and any values that it returns are passed back to the calling code.\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", + "In **this exercise**, we have defined a function called ``encounter_ghost`` that ``simulates a battle between the adventurer and a ghost``, and ``run_mansion``. \n", + "- Your task is to **complete these functions** by **using flow control statements** such as ``if``, ``else``, ``while``, ``for loops``, and ``nested loops``. \n", + "- Remember to pay attention to the instructions and comments provided in the function to help guide you. \n", + "- 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", - " 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", + "Good luck!\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!" + "---" ] }, { @@ -82,18 +92,51 @@ "metadata": {}, "outputs": [], "source": [ + "\"\"\"\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", + " \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", + " \n", + " If the random number is greater than 5, the adventurer loses the battle. \n", + " In this case, print \"You lost the battle...\"\n", + " \n", " and return something that indicates the ghost defeated the adventurer.\n", - " \"\"\"\n", + "\n", " print(\"You encounter a ghost!\")\n", + "\"\"\" \n", + "\n", + "\n", + "\n", + "# your code goes here\n", + "\n", + "import random\n", + "def encounter_ghost():\n", + "\n", + " print(\"You encounter a ghost!\")\n", + " # handles the encounter with a ghost ---------------------------------------------------------------------------\n", + "\n", + " # The outcome of the battle is determined by a random number between 1 and 10\n", + " number = random.randint(1, 10) # random.randint(a, b) generates a random integer N such that a <= N <= b\n", + "\n", + " # If the random number is less than or equal to 5, \n", + " if number <= 5: \n", + " battle = \"won battle\" \n", + " # the adventurer defeats the ghost, print the message \"You defeated the ghost!\"\n", + " print(\"\\nYou defeated the ghost!\")\n", + " \n", " \n", - " # your code goes here" + " # If the random number is greater than 5, \n", + " else :\n", + " battle = \"lost battle\"\n", + " # the adventurer loses the battle, print the message \"You lost the battle...\"\n", + " print(\"\\nYou lost the battle...\") \n", + " \n", + " # return something that indicates the adventurer defeated the ghost or lost the battle\n", + " return battle\n" ] }, { @@ -104,11 +147,11 @@ "outputs": [], "source": [ "# main function for the game\n", - "def run_mansion():\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", " Prompt the user to choose between two paths: \"left\" or \"right\". \n", "\n", @@ -130,7 +173,115 @@ "\n", " \"\"\"\n", " \n", - " # your code goes here" + "# your code goes here\n", + "# ===============================================================================================================================\n", + "# GAME MAIN FUNCTION : run_mansion() --------------------------------------------------------------------------------------------\n", + "# ===============================================================================================================================\n", + "# Simulates an adventure through a haunted mansion ------------------------------------------------------------------------------\n", + "# ===============================================================================================================================\n", + "import random\n", + "mansion_items = [\"sword\", \"shield\", \"magic wand\", \"cloak\", \"potion\", \"map\", \"lantern\", \"amulet\", \"ring\"]\n", + "\n", + "def run_mansion():\n", + " \n", + " print(\"Welcome to the Haunted Mansion!\") \n", + " # Adventurer starts with 10 health points and no items\n", + " health = 10 # initial health points\n", + " items = [] # empty items list\n", + " intersections = 0 # initialize count number of intersections\n", + " challenges = 0 # initialize count number of challenges overcome\n", + " mansion_challenges = [\"solve a riddle\", \"cross a rickety bridge\", \"find a hidden lever\", \"dodge swinging axes\", \"navigate a maze\"] # list of challenges in the mansion \n", + " \n", + " # ===============================================================================================================================\n", + " # MAIN GAME LOOP: keep going while alive and treasure not found------------------------------------------------------------------\n", + " # ===============================================================================================================================\n", + "\n", + " while health > 0 and \"treasure\" not in items:\n", + " \n", + " # Prompt to choose between two paths: \"left\" or \"right\"\n", + " path = input(\"\\nChoose your path (left/right): \").lower()\n", + " while(path != \"left\" and path != \"right\"):\n", + " path = input(\"\\nInvalid choice. Please try again. left/right: \").lower()\n", + " \n", + " # Within run_mansion(), \n", + " ## *prompt* to *choose a path* to take *at each intersection*\n", + " ### Each *path* has its unique *challenges* and *obstacles* that *the adventurer must overcome* \n", + " \n", + " intersections += 1 # intersections count\n", + "\n", + " # depending on the path chosen, different events occur ----------------------------------------------------------------------\n", + " # LEFT PATH ---------------------------------------------------------------------------------------------------------------- \n", + " if path == \"left\":\n", + " print(\"\\nAdventurer chose left path.\")\n", + " \n", + " # a random event occurs, selects either \"potion\" or \"trap\" 50% chance each\n", + " event = random.choice([\"potion\", \"trap\"]) \n", + " \n", + " # If they find the **potion**, it is saved into the adventurer's **items**.\n", + " if event == \"potion\":\n", + " items.append(\"potion\") # add potion to items list\n", + " challenge = random.choice(mansion_challenges) # choose a random challenge from mansion_challenges list\n", + " print(f\"\\nYou had to {challenge} to find the potion.\")\n", + " challenges += 1 # increment challenges count\n", + " print(f\"\\nYou found {event}! It has been added to your items.\")\n", + " print(f\"\\nYour current items are : {items}\")\n", + " \n", + "\n", + " # If they fall into a **trap**, **2 points are taken out of** the adventurer's **health** points.\n", + " elif event == \"trap\":\n", + " health = health - 2\n", + " print(\"\\nYou fell into a trap! You lost 2 health points.\")\n", + " print(f\"\\nYour current health is : {health}\")\n", + "\n", + " # RIGHT PATH ---------------------------------------------------------------------------------------------------------------- \n", + " \n", + " elif path == \"right\":\n", + " print(\"\\nAdventurer chose right path.\")\n", + " battle_outcome = encounter_ghost()\n", + " # If adventurer wins, find a key\n", + " if battle_outcome == \"won battle\":\n", + " items.append(\"key\") # save key in items list\n", + " print(\"\\nYou found a key! It has been added to your items.\")\n", + " print(f\"\\nYour current items are : {items}\")\n", + " # If adventurer loses, health -2 points\n", + " elif battle_outcome == \"lost battle\":\n", + " health = health - 2\n", + " print(\"\\nYou lost 2 health points.\")\n", + " print(f\"\\nYour current health is : {health}\") \n", + " \n", + " # else adventurer chooses something other than \"left\" or \"right\" -------------------------------------------------------------\n", + " else :\n", + " while path != \"left\" and path != \"right\":\n", + " path = input(\"\\nInvalid choice. Choose a path (left/right): \").lower() # prompt again until left or right is inputed\n", + " \n", + " # Check game‑ending conditions after each step -----------------------------\n", + " if health <= 0:\n", + " break \n", + "\n", + " # if key in items,adventurer has key ----------------------------------------------------------------------------------------\n", + " # adventurer can unlock the door and find the Treasure\n", + " if \"key\" in items:\n", + " items.append(\"treasure\") # add treasure to items list\n", + " print(\"\\nYou unlocked the door and found the Treasure! Congratulations!\")\n", + " break # exit the while loop since treasure is found\n", + "\n", + " # else they don't have the key, \n", + " else:\n", + " print(\"\\nYou don't have the key to unlock the door.\")\n", + " print(\"\\nYou decide to keep exploring the mansion...\")\n", + " # prompt to find it from the beginning\n", + "# ======================================================================================================================================\n", + "# OUTSIDE MAIN LOOP ------------------------------------------------------------------------------------------\n", + "# If the adventurer's health points reach 0 or less ================================================================================\n", + " if health <= 0:\n", + " print(\"\\nGame over! you lost all your health points.\")\n", + "\n", + " elif \"treasure\" in items:\n", + " print(\"\\nYou escape the Haunted Mansion with the Treasure!\")\n", + " print(f\"Total intersections visited: {intersections}\")\n", + " print(f\"Total challenges overcome: {challenges}\")\n", + "# ======================================================================================================================================\n", + "\n" ] }, { @@ -158,11 +309,366 @@ "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. " ] + }, + { + "cell_type": "markdown", + "id": "85d3814f", + "metadata": {}, + "source": [ + "---\n", + "# Solved Lab | Flow Control EXTRA" + ] + }, + { + "cell_type": "markdown", + "id": "7d3cf5c5", + "metadata": {}, + "source": [ + "---\n", + "1. 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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dbdd51d4", + "metadata": {}, + "outputs": [], + "source": [ + "mansion_items = [\"sword\", \"shield\", \"magic wand\", \"cloak\", \"potion\", \"map\", \"lantern\", \"amulet\", \"ring\"]\n", + "print(f\"list of items to help the adventurer overcome obstacles and defeat ghosts:\\n{mansion_items}\")" + ] + }, + { + "cell_type": "markdown", + "id": "c8053619", + "metadata": {}, + "source": [ + "---\n", + "2. Complete the function called \"**run_mansion()**\" that **serves as the main function for the game**.\n", + "\n", + "Within \"**run_mansion()**\", **prompt the user to choose a path to take at each intersection**. \n", + "\n", + "Each path should have its unique **challenges** and **obstacles** that **the adventurer must overcome**." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2c0e8aec", + "metadata": {}, + "outputs": [], + "source": [ + "# ===============================================================================================================================\n", + "# GAME MAIN FUNCTION : run_mansion() --------------------------------------------------------------------------------------------\n", + "# ===============================================================================================================================\n", + "# Simulates an adventure through a haunted mansion ------------------------------------------------------------------------------\n", + "# ===============================================================================================================================\n", + "import random\n", + "# list of challenges in the mansion\n", + "mansion_challenges = [\"solve a riddle\", \"cross a rickety bridge\", \"find a hidden lever\", \"dodge swinging axes\", \"navigate a maze\"] \n", + "# list of items in the mansion\n", + "mansion_items = [\"sword\", \"shield\", \"magic wand\", \"cloak\", \"potion\", \"map\", \"lantern\", \"amulet\", \"ring\"]\n", + "\n", + "def run_mansion():\n", + " \n", + " print(\"Welcome to the Haunted Mansion!\") \n", + " # Adventurer starts with 10 health points and no items\n", + " health = 10 # initial health points\n", + " items = [] # empty items list\n", + " intersections = 0 # initialize count number of intersections\n", + " challenges = 0 # initialize count number of challenges overcome\n", + "\n", + " # ===============================================================================================================================\n", + " # MAIN GAME LOOP: keep going while alive and treasure not found------------------------------------------------------------------\n", + " # ===============================================================================================================================\n", + "\n", + " while health > 0 and \"treasure\" not in items:\n", + " \n", + " # Prompt to choose between two paths: \"left\" or \"right\"\n", + " path = input(\"\\nChoose your path (left/right): \").lower()\n", + " while(path != \"left\" and path != \"right\"):\n", + " path = input(\"\\nInvalid choice. Please try again. left/right: \").lower()\n", + " \n", + " # Within run_mansion(), \n", + " ## *prompt* to *choose a path* to take *at each intersection*\n", + " ### Each *path* has its unique *challenges* and *obstacles* that *the adventurer must overcome* \n", + " \n", + " intersections += 1 # intersections count\n", + "\n", + " # depending on the path chosen, different events occur ----------------------------------------------------------------------\n", + " # LEFT PATH ---------------------------------------------------------------------------------------------------------------- \n", + " if path == \"left\":\n", + " print(\"\\nAdventurer chose left path.\")\n", + " \n", + " # a random event occurs, selects either \"potion\" or \"trap\" 50% chance each\n", + " event = random.choice([\"potion\", \"trap\"]) \n", + " \n", + " # If they find the **potion**, it is saved into the adventurer's **items**.\n", + " if event == \"potion\":\n", + " items.append(\"potion\") # add potion to items list\n", + " challenge = random.choice(mansion_challenges) # choose a random challenge from mansion_challenges list\n", + " print(f\"\\nYou had to {challenge} to find the potion.\")\n", + " challenges += 1 # increment challenges count\n", + " print(f\"\\nYou found {event}! It has been added to your items.\")\n", + " print(f\"\\nYour current items are : {items}\")\n", + " \n", + "\n", + " # If they fall into a **trap**, **2 points are taken out of** the adventurer's **health** points.\n", + " elif event == \"trap\":\n", + " health = health - 2\n", + " print(\"\\nYou fell into a trap! You lost 2 health points.\")\n", + " print(f\"\\nYour current health is : {health}\")\n", + "\n", + " # RIGHT PATH ---------------------------------------------------------------------------------------------------------------- \n", + " \n", + " elif path == \"right\":\n", + " print(\"\\nAdventurer chose right path.\")\n", + " battle_outcome = encounter_ghost()\n", + " # If adventurer wins, find a key\n", + " if battle_outcome == \"won battle\":\n", + " items.append(\"key\") # save key in items list\n", + " print(\"\\nYou found a key! It has been added to your items.\")\n", + " print(f\"\\nYour current items are : {items}\")\n", + " # If adventurer loses, health -2 points\n", + " elif battle_outcome == \"lost battle\":\n", + " health = health - 2\n", + " print(\"\\nYou lost 2 health points.\")\n", + " print(f\"\\nYour current health is : {health}\") \n", + " \n", + " # else adventurer chooses something other than \"left\" or \"right\" -------------------------------------------------------------\n", + " else :\n", + " while path != \"left\" and path != \"right\":\n", + " path = input(\"\\nInvalid choice. Choose a path (left/right): \").lower() # prompt again until left or right is inputed\n", + " \n", + " # Check game‑ending conditions after each step -----------------------------\n", + " if health <= 0:\n", + " break \n", + "\n", + " # if key in items,adventurer has key ----------------------------------------------------------------------------------------\n", + " # adventurer can unlock the door and find the Treasure\n", + " if \"key\" in items:\n", + " items.append(\"treasure\") # add treasure to items list\n", + " print(\"\\nYou unlocked the door and found the Treasure! Congratulations!\")\n", + " break # exit the while loop since treasure is found\n", + "\n", + " # else they don't have the key, \n", + " else:\n", + " print(\"\\nYou don't have the key to unlock the door.\")\n", + " print(\"\\nYou decide to keep exploring the mansion...\")\n", + " # prompt to find it from the beginning\n", + "# ======================================================================================================================================\n", + "# OUTSIDE MAIN LOOP ------------------------------------------------------------------------------------------\n", + "# If the adventurer's health points reach 0 or less ================================================================================\n", + " if health <= 0:\n", + " print(\"\\nGame over! you lost all your health points.\")\n", + "# if treasure in items,adventurer wins ----------------------------------------------------------------------------------------\n", + " elif \"treasure\" in items:\n", + " print(\"\\nYou escape the Haunted Mansion with the Treasure!\")\n", + " print(f\"Total intersections visited: {intersections}\")\n", + " print(f\"Total challenges overcome: {challenges}\")\n", + "# ======================================================================================================================================\n" + ] + }, + { + "cell_type": "markdown", + "id": "55b85c57", + "metadata": {}, + "source": [ + "---\n", + "3. Use **loops** to **check if** the adventurer has **enough health points** to continue the game. \n", + "\n", + "If their health points drop to zero, the game is over." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "544a6c5b", + "metadata": {}, + "outputs": [], + "source": [ + "while(health>0):\n", + " if health <= 0:\n", + " print(\"\\nGame over! you lost all your health points.\")\n", + " break" + ] + }, + { + "cell_type": "markdown", + "id": "2bef06bf", + "metadata": {}, + "source": [ + "---\n", + "4. Complete the function called \"**encounter_ghost()**\" that will be **used to handle ghost encounters**. \n", + "\n", + "- The function should **use random events** to **determine** the **outcome of the encounter**, \n", + "- and the **adventurer** should **use** their **items** to help them **defeat the ghost**." + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "5d07e6e3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You encounter a ghost!\n", + "Your sword empower you!\n", + "Roll: 4 + 2 bonus = 6\n", + "\n", + "You lost the battle...\n" + ] + } + ], + "source": [ + "import random\n", + "\n", + "def encounter_ghost(items: list):\n", + " \"\"\"\n", + " Handles ghost encounters where adventurer uses their items to influence outcome\n", + " Returns \"won battle\" or \"lost battle\" based on random roll + item bonuses\n", + " \"\"\"\n", + " print(\"You encounter a ghost!\")\n", + "\n", + " # Random base roll (1-10) determines encounter difficulty\n", + " base_roll = random.randint(1, 10)\n", + "\n", + " # Calculate bonus from helpful items\n", + " bonus = 0\n", + " helpful_items = []\n", + " \n", + " if \"sword\" in items:\n", + " bonus += 2\n", + " helpful_items.append(\"sword\")\n", + " elif \"magic wand\" in items:\n", + " bonus += 3\n", + " helpful_items.append(\"magic wand\")\n", + " elif \"shield\" in items:\n", + " bonus += 1\n", + " helpful_items.append(\"shield\")\n", + " elif \"potion\" in items:\n", + " bonus += 1\n", + " helpful_items.append(\"potion\") # potions help too!\n", + "\n", + " # Show which items are helping\n", + " if helpful_items:\n", + " print(f\"Your {', '.join(helpful_items)} empower you!\")\n", + " else:\n", + " print(\"No helpful items... good luck!\")\n", + "\n", + " # Final roll = random event + item power\n", + " final_roll = base_roll + bonus\n", + " print(f\"Roll: {base_roll} + {bonus} bonus = {final_roll}\") # optional: show math\n", + "\n", + " # Win if final roll meets/exceeds threshold\n", + " if final_roll >= 7: # slightly higher threshold to make items matter more\n", + " print(\"\\nYou defeated the ghost!\")\n", + " return \"won battle\"\n", + " else:\n", + " print(\"\\nYou lost the battle...\")\n", + " return \"lost battle\"\n", + "outcome = encounter_ghost([\"sword\", \"potion\"])" + ] + }, + { + "cell_type": "markdown", + "id": "f2e45d7b", + "metadata": {}, + "source": [ + "---\n", + "5. Use loops to **generate random events** or **items along the way**.\n", + "\n", + "These **events** can **either help or hinder the adventurer**, and **the outcome** should be **based on random chance**." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d6d09f9e", + "metadata": {}, + "outputs": [], + "source": [ + "# generate *random events* or *random items* along the way\n", + "# an *event* can either be *beneficial* (like finding an item) _or_ *detrimental* (like falling into a trap)\n", + "# *outcome* based on *random chance*\n", + "\n", + "import random\n", + "def random_event():\n", + " event_type = random.choice([\"beneficial\", \"detrimental\"])\n", + " if event_type == \"beneficial\":\n", + " challenge = random.choice(mansion_challenges)\n", + " item_found = random.choice(mansion_items)\n", + " print(f\"You had to {challenge}\")\n", + " print(f\"You found a {item_found}!\")\n", + " return (\"beneficial\", item_found)\n", + " else:\n", + " damage = random.randint(1, 3)\n", + " print(f\"You fell into a trap and lost {damage} health points!\")\n", + " return (\"detrimental\", damage)" + ] + }, + { + "cell_type": "markdown", + "id": "351259a5", + "metadata": {}, + "source": [ + "---\n", + "6. At the **end of the mansion**, the **adventurer** will **find the treasure**, and **the game will end**." + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "id": "c6670a43", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰\n", + "============================================================\n", + " You reach the end of the mansion... \n", + " ✨ The treasure chest glows before you! ✨ \n", + "============================================================\n", + " πŸ† ADVENTURE COMPLETE πŸ† \n", + " TREASURE FOUND: Ancient Crown \n", + "============================================================\n", + "πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰\n" + ] + } + ], + "source": [ + "# Center text function (width=60)\n", + "def center_print(text):\n", + " print(text.center(60))\n", + "\n", + "# END OF MANSION - TREASURE FOUND random\n", + "\n", + "import random\n", + "treasures = [\"Golden Idol\", \"Magic Amulet\", \"Ancient Crown\", \"Jeweled Dagger\", \"Enchanted Orb\"]\n", + "treasure_found = random.choice(treasures)\n", + "print(\"πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰\")\n", + "\n", + "print(\"=\"*60)\n", + "center_print(\"You reach the end of the mansion...\")\n", + "center_print(\"✨ The treasure chest glows before you! ✨\")\n", + "print(\"=\"*60)\n", + "center_print(\"πŸ† ADVENTURE COMPLETE πŸ†\")\n", + "center_print(f\"TREASURE FOUND: {treasure_found}\")\n", + "print(\"=\"*60)\n", + "\n", + "print(\"πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰\")\n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -176,7 +682,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,