diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..82cf0f5 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -75,27 +75,6 @@ " Good luck!" ] }, - { - "cell_type": "code", - "execution_count": null, - "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", - " # your code goes here" - ] - }, { "cell_type": "code", "execution_count": null, @@ -103,66 +82,228 @@ "metadata": {}, "outputs": [], "source": [ - "# main function for the game\n", "def run_mansion():\n", - " \n", " print(\"Welcome to the Haunted Mansion!\")\n", + "\n", + " def character_features():\n", + " inventory = {'Lifes': 10, 'Rosario': 0, 'Scooby Doo': 0, 'Potion': 0}\n", + " game_status = {'won': False, 'dead': False, 'restart': False}\n", + "\n", + " if game_status['restart'] == True:\n", + " inventory = {'Lifes': 10, 'Rosario': 0, 'Scooby Doo': 0, 'Potion': 0}\n", + "\n", + " return inventory, game_status \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", + " def choose_path(inventory,game_status):\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", + " print(\"Choose wisely. For which path do you want to risk your life?: 'right' or 'left'? 👻 \")\n", + " \n", + " while game_status['won'] == False:\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", + " if game_status['dead'] == True:\n", + " break\n", + " \n", + " user_choise = input(\"Type Right or Left\").lower().strip()\n", "\n", - " If the adventurer chooses something other than \"left\" or \"right\", they are prompted to try again.\n", + " if user_choise == \"left\":\n", + " left_path()\n", + " elif user_choise == 'right':\n", + " right_path(inventory)\n", + " else:\n", + " print('Invalid input') \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", + " def use_potion(inventory):\n", + " # Revisar salidas de bucles\n", + " if inventory['Potion'] > 0:\n", + " exit_function = 'no'\n", + " while exit_function == 'no':\n", + " print ('Do you want to use a Potion.')\n", + " use_potion = input('Yes or No').lower().strip()\n", + " if use_potion == 'yes':\n", + " print(f'You already have {inventory[\"Potion\"]} potion(s). How many potions do you want to use')\n", + " \n", + " while True:\n", + " try:\n", + " \n", + " potions_to_use = int(input('Type the number'))\n", + " if potions_to_use < 0:\n", + " raise Exception('Numbers cannot be negative')\n", + " elif potions_to_use > inventory['Potion']:\n", + " print('You do not have enought')\n", + " else:\n", + " inventory['Potion'] -= potions_to_use\n", + " inventory['Lifes'] -= potions_to_use\n", + " \n", + " except ValueError:\n", + " print(f'Invalid input')\n", + " except Exception as e:\n", + " print(f'Error: {e}')\n", + " else:\n", + " break\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", + " exit_function = 'yes'\n", "\n", - " \"\"\"\n", - " \n", - " # your code goes here" - ] - }, - { - "cell_type": "markdown", - "id": "9e13a33c-38e5-44b3-bd1b-9a642c962c89", - "metadata": {}, - "source": [ - "To run the game, simply call the run_mansion() function:" + " elif use_potion == 'no':\n", + " if inventory['Lifes'] <= 2:\n", + " print(f'You already have {inventory[\"Lifes\"]} lifes. If you do not use one of them you could die in the next turn.')\n", + " print('Do you want to use it?')\n", + " while True:\n", + " use_potion = input('Yes or No')\n", + " if use_potion == 'yes':\n", + " inventory['Lifes'] -= 1\n", + " inventory['Potion'] -= 1\n", + " print(f'You already have {inventory[\"Lifes\"]} life')\n", + " break\n", + " elif use_potion == 'no':\n", + " break\n", + " else: \n", + " print('Invalid input')\n", + "\n", + " exit_function = 'yes'\n", + " \n", + " # indicar vidas restantes y actuar en consecuencia \n", + " else:\n", + " inventory['Potion'] += 0\n", + " exit_function = 'yes'\n", + " else:\n", + " print('Invalid input')\n", + "\n", + " continue_playing(inventory)\n", + "\n", + " else:\n", + " continue_playing(inventory)\n", + "\n", + " def encounter_ghost(inventory):\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", + " import random\n", + " print(\"You encounter a ghost!\")\n", + " battle_decision = random.randint(1,10)\n", + " if battle_decision in range(1,6):\n", + " print('And you defeat it')\n", + " game_status['won'] = True\n", + " continue_playing(inventory)\n", + " elif battle_decision in range(6,11):\n", + " print('You lost the battle and lose 2 lifes')\n", + " inventory['Lifes'] -= 2\n", + " use_potion(inventory)\n", + "\n", + " def right_path(inventory):\n", + " \n", + " import random\n", + "\n", + " ghost_or_price = random.randint(1,10)\n", + "\n", + " if ghost_or_price in range(1,6):\n", + " print(\"Congratulation you skipped the death\")\n", + "\n", + " price_won = random.randint(1,10)\n", + " \n", + " if price_won == 1:\n", + " print('You have won Scooby Doo')\n", + " inventory['Scooby Doo'] += 1\n", + " continue_playing(inventory)\n", + " elif price_won in range(2,6):\n", + " print('You have won a Rosario')\n", + " inventory['Rosario'] += 1\n", + " continue_playing(inventory)\n", + " else:\n", + " print(f'You have won a potion. You already have {inventory[\"Lifes\"]} lifes')\n", + " inventory['Potion'] += 1\n", + " use_potion(inventory)\n", + " else:\n", + " print (\"You fall in a trap and lose 2 health points\")\n", + " inventory['Lifes'] -= 2\n", + " if inventory['Potion'] > 0:\n", + " print(f'Do you already have {inventory[\"Lifes\"]} life(s)')\n", + " use_potion(inventory)\n", + " else:\n", + " continue_playing(inventory)\n", + " \n", + " # return inventory\n", + " # dar opcion de usar poción si tiene para contrarestar una vida\n", + " # restar dos vidas o una de función character_features()\n", + "\n", + " def left_path():\n", + " encounter_ghost(inventory)\n", + "\n", + " def continue_playing(inventory):\n", + " \n", + " if inventory['Lifes'] > 0 and game_status['won'] == False:\n", + " user_choise = input(\"Try again. Type Right or Left\").lower().strip()\n", + "\n", + " while user_choise not in (\"left\",\"right\"):\n", + " print(\"Controla tus dedos, no dejan de temblar. Choose a correct option\")\n", + " user_choise = input(\"Type Right or Left\").lower().strip()\n", + "\n", + " if user_choise == \"left\":\n", + " left_path()\n", + " else:\n", + " right_path(inventory)\n", + " elif game_status['won'] == True:\n", + " print('Congratulation, you won the battle againt the ghost and found the master key')\n", + " print('You have won')\n", + " choose_path(inventory,game_status)\n", + " elif inventory['Lifes'] <= 0:\n", + " print('Game over. You have lose all your lifes.')\n", + " print('Do you want still playing?: (Yes/No)')\n", + " user_choise = input('Type Yes or No:').lower().strip()\n", + " if user_choise == 'yes':\n", + " game_status['restart'] = True\n", + " character_features()\n", + " elif user_choise == 'no':\n", + " game_status['dead'] = True\n", + " \n", + " inventory, game_status = character_features()\n", + " choose_path(inventory,game_status) " ] }, { "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", + "Choose wisely. For which path do you want to risk your life?: 'right' or 'left'? 👻 \n", + "Congratulation you skipped the death\n", + "You have won a Rosario\n", + "You fall in a trap and lose 2 health points\n", + "You fall in a trap and lose 2 health points\n", + "You fall in a trap and lose 2 health points\n", + "You fall in a trap and lose 2 health points\n", + "You fall in a trap and lose 2 health points\n", + "Game over. You have lose all your lifes.\n", + "Do you want still playing?: (Yes/No)\n" + ] + } + ], "source": [ "run_mansion()" ] }, { - "cell_type": "markdown", - "id": "88212f63-3bdb-479f-bf6c-4ecd0685d39a", + "cell_type": "code", + "execution_count": null, + "id": "494a95fa", "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. " - ] + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -176,7 +317,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.4" } }, "nbformat": 4,