diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..591312b 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -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." ] }, { @@ -77,12 +77,50 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "499552c8-9e30-46e1-a706-4ac5dc64670e", + "execution_count": 1, + "id": "0db82139", "metadata": {}, "outputs": [], + "source": [ + "import random" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "85f86e54", + "metadata": {}, + "outputs": [], + "source": [ + "# Begin by creating a list of items that the adventurer can pick up along the way. \n", + "# These items will be used to help the adventurer overcome obstacles and defeat ghosts. \n", + "# Examples of items can be weapons, potions, keys, etc.\n", + "backpack = []\n", + "health_points = 10" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "499552c8-9e30-46e1-a706-4ac5dc64670e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You encounter a ghost!\n", + "The ghost attacks\n", + "You lost the battle!\n", + "You loose 2 health_points.\n", + "Health points: 6\n", + "Checking ghost encounter logic: False\n" + ] + } + ], "source": [ "def encounter_ghost():\n", + " global health_points\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", @@ -91,21 +129,37 @@ " 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", - " # your code goes here" + " print(\"You encounter a ghost!\\nThe ghost attacks\")\n", + " random_number = random.randint(1, 10)\n", + "\n", + " #win or loose:\n", + " if 1<= random_number <=5:\n", + " print(\"You defeated the ghost!\")\n", + " return True\n", + " else:\n", + " print(\"You lost the battle!\\nYou loose 2 health_points.\")\n", + " health_points -= 2\n", + " print(f'Health points: {health_points}')\n", + " if health_points <= 0:\n", + " print(\"GAME OVER! You lost all you health points\")\n", + " return False\n", + "\n", + "print(\"Checking ghost encounter logic:\", encounter_ghost())" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "d3e4076b-48cc-41ac-95ad-891743e775f5", "metadata": {}, "outputs": [], "source": [ "# main function for the game\n", + "# Complete the function called \"run_mansion()\" that serves as the main function for the game. \n", + "# Within \"run_mansion()\", prompt the user to choose a path to take at each intersection. \n", + "# Each path should have its unique challenges and obstacles that the adventurer must overcome.\n", "def run_mansion():\n", - " \n", + " global health_points\n", " print(\"Welcome to the Haunted Mansion!\")\n", " \n", " \"\"\"\n", @@ -118,7 +172,7 @@ "\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", + " Hint: remember to check what encounter_ghost() return 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", @@ -129,8 +183,144 @@ " If they don't have the key, they are prompted to find it from the beginning.\n", "\n", " \"\"\"\n", - " \n", - " # your code goes here" + " print(\"You enter the Haunted Mansion through a back window.\\nYou find yourself in the main kitchen.\")\n", + " #create a loop to ask where to go\n", + " #loop 1\n", + " while True: \n", + " where_to_go = input(\"Are you going left or right?\").strip().lower() \n", + " #give instructions what happened after turning left\n", + " if where_to_go == 'left':\n", + " #random 50\\50 %\n", + " if random.choice([True, False]):\n", + " print(\"You turn left and enter the cooks quarters, you find a back of salt\")\n", + " backpack.append('Salt')\n", + " print(f'Backpack: {backpack}')\n", + " else: \n", + " print(\"You turn left and enter the cooks quarters.\\nA black cat with glowing eyes jumps out at you hissing!\\nYou suffer a minor heart attack and loose 2 health points\") \n", + " health_points -= 2\n", + " print(f'Health points: {health_points}')\n", + " if health_points <= 0:\n", + " print(\"GAME OVER! You lost all you health points\")\n", + " return\n", + " break\n", + " #give instructions on what happens after turning right\n", + " elif where_to_go == 'right':\n", + " if encounter_ghost() == True:\n", + " print(\"You find a key!\\nyou unlock the door and find the Treasure!\")\n", + " return\n", + " else:\n", + " break\n", + " #give instructions to pick left or right\n", + " else:\n", + " print(\"please choose 'left' or 'right'!\")\n", + " #loop 2 \n", + " while True:\n", + " where_to_go_next = input(\"Where would you like to go next: left or right?\")\n", + " if where_to_go_next == 'left':\n", + " if random.choice([True, False]):\n", + " print(\"You turn left and enter a long dark hall way.\\nYou find a dagger.\")\n", + " backpack.append('Dagger') \n", + " print(f'Backpack: {backpack}')\n", + " \n", + " else: \n", + " print(\"You turn left and enter a long dark hall way.\\nThe suits of armor against the wall come to life and start fighting you.\\nYou get stabbed in the arm and loose 2 health points\") \n", + " health_points -= 2\n", + " print(f'Health points: {health_points}') \n", + " if health_points <= 0:\n", + " print(\"GAME OVER! You lost all you health points\")\n", + " return\n", + " break\n", + " #give instructions on what happens after turning right\n", + " elif where_to_go_next == 'right':\n", + " if encounter_ghost() == True:\n", + " print(\"You find a key!\\nyou unlock the door and find the Treasure!\")\n", + " return\n", + " else:\n", + " break\n", + " #give instructions to pick left or right\n", + " else:\n", + " print(\"please choose 'left' or 'right'!\")\n", + "#loop 3 \n", + " while True:\n", + " where_to_go_next = input(\"Where would you like to go next: left or right?\")\n", + " if where_to_go_next == 'left':\n", + " if random.choice([True, False]):\n", + " print(\"You turn left and enter an old dusty library.\\nYou find a treasure map.\")\n", + " backpack.append('Treasure map') \n", + " print(f'Backpack: {backpack}')\n", + " \n", + " else: \n", + " print(\"You turn left and enter an old dusty library.\\nYou try to turn on the lights but you get shocked! You loose 2 health points\") \n", + " health_points -= 2\n", + " print(f'Health points: {health_points}') \n", + " if health_points <= 0:\n", + " print(\"GAME OVER! You lost all you health points\")\n", + " return\n", + " break\n", + " #give instructions on what happens after turning right\n", + " elif where_to_go_next == 'right':\n", + " if encounter_ghost() == True:\n", + " print(\"You find a key!\\nyou unlock the door and find the Treasure!\")\n", + " return\n", + " else:\n", + " break\n", + " #give instructions to pick left or right\n", + " else:\n", + " print(\"please choose 'left' or 'right'!\")\n", + "#loop 4 \n", + " while True:\n", + " where_to_go_next = input(\"Where would you like to go next: left or right?\")\n", + " if where_to_go_next == 'left':\n", + " if random.choice([True, False]):\n", + " print(\"You turn left and find a hidden door, you go through find a network of secret tunnels.\\nYou find a torch\")\n", + " backpack.append('Torch') \n", + " print(f'Backpack: {backpack}')\n", + " \n", + " else: \n", + " print(\"You turn left and find a hidden door, you go through find a network of secret tunnels.\\nYou find a torch, but when you try to grab it spike come out of the walls. You loose 2 health points\") \n", + " health_points -= 2\n", + " print(f'Health points: {health_points}') \n", + " if health_points <= 0:\n", + " print(\"GAME OVER! You lost all you health points\")\n", + " return\n", + " break\n", + " #give instructions on what happens after turning right\n", + " elif where_to_go_next == 'right':\n", + " if encounter_ghost() == True:\n", + " print(\"You find a key!\\nyou unlock the door and find the Treasure!\")\n", + " return\n", + " else:\n", + " break\n", + " #give instructions to pick left or right\n", + " else:\n", + " print(\"please choose 'left' or 'right'!\")\n", + "#loop 5 \n", + " while True:\n", + " where_to_go_next = input(\"Where would you like to go next: left or right?\")\n", + " if where_to_go_next == 'left':\n", + " if random.choice([True, False]):\n", + " print(\"You turn left and step out into a circular room with a stone altar in the center.\\nYou walk to the altar and find gemstones strewn on top of it.\")\n", + " backpack.append('Gemstones') \n", + " print(f'Backpack: {backpack}')\n", + " \n", + " else: \n", + " print(\"You turn left and step out into a circular room with a stone altar in the center.\\nYou walk to the altar and step on a trap stone.\\nWith a bang a huge bould closes the entrence you just came through. You loose 2 health point\") \n", + " health_points -= 2\n", + " print(f'Health points: {health_points}') \n", + " if health_points <= 0:\n", + " print(\"GAME OVER! You lost all you health points\")\n", + " return\n", + " #give instructions on what happens after turning right\n", + " elif where_to_go_next == 'right':\n", + " if encounter_ghost() == True:\n", + " print(\"You find a key!\\nYou unlock the door and find the Treasure!\")\n", + " return\n", + " else:\n", + " break\n", + " #give instructions to pick left or right\n", + " else:\n", + " print(\"please choose 'left' or 'right'!\")\n", + "\n" ] }, { @@ -143,10 +333,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Haunted Mansion!\n", + "You enter the Haunted Mansion through a back window.\n", + "You find yourself in the main kitchen.\n", + "You turn left and enter the cooks quarters.\n", + "A black cat with glowing eyes jumps out at you hissing!\n", + "You suffer a minor heart attack and loose 2 health points\n", + "Health points: 4\n", + "You turn left and enter a long dark hall way.\n", + "You find a dagger.\n", + "Backpack: ['Dagger']\n", + "You turn left and enter an old dusty library.\n", + "You try to turn on the lights but you get shocked! You loose 2 health points\n", + "Health points: 2\n", + "You encounter a ghost!\n", + "The ghost attacks\n", + "You defeated the ghost!\n", + "You find a key!\n", + "you unlock the door and find the Treasure!\n" + ] + } + ], "source": [ "run_mansion()" ] @@ -162,7 +377,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -176,7 +391,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,