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
131 changes: 88 additions & 43 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -92,45 +92,100 @@
" and return something that indicates the ghost defeated the adventurer.\n",
" \"\"\"\n",
" print(\"You encounter a ghost!\")\n",
" \n",
" # your code goes here"
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"execution_count": 2,
"id": "7155d798",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"¡Bienvenido a la Mansión Encantada!\n",
"Salud actual: 10 HP | Objetos: []\n",
"¡Te encuentras con un fantasma!\n",
"Has perdido la batalla...\n",
"Salud actual: 8 HP | Objetos: []\n",
"¡Oh no! Has caído en una trampa.\n",
"Salud actual: 6 HP | Objetos: []\n",
"¡Has encontrado una poción!\n",
"Salud actual: 6 HP | Objetos: ['Poción']\n",
"¡Oh no! Has caído en una trampa.\n",
"Salud actual: 4 HP | Objetos: ['Poción']\n",
"¡Te encuentras con un fantasma!\n",
"Has perdido la batalla...\n",
"Salud actual: 2 HP | Objetos: ['Poción']\n",
"¡Te encuentras con un fantasma!\n",
"¡Has derrotado al fantasma!\n",
"Has encontrado una llave dorada.\n",
"\n",
"¡Has usado la llave para abrir el cofre del tesoro!\n",
"¡Felicidades! Has escapado de la mansión con el tesoro.\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",
"import random\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",
"def encounter_ghost():\n",
" \"\"\"Gestiona el encuentro con un fantasma y devuelve el resultado.\"\"\"\n",
" print(\"¡Te encuentras con un fantasma!\")\n",
" numero_aleatorio = random.randint(1, 10)\n",
" \n",
" if numero_aleatorio <= 5:\n",
" print(\"¡Has derrotado al fantasma!\")\n",
" return True\n",
" else:\n",
" print(\"Has perdido la batalla...\")\n",
" return False\n",
"\n",
" \"\"\"\n",
" \n",
" # your code goes here"
"def run_mansion():\n",
" \"\"\"Función principal que controla la aventura.\"\"\"\n",
" print(\"¡Bienvenido a la Mansión Encantada!\")\n",
" vida = 10\n",
" objetos = []\n",
" tesoro_encontrado = False\n",
" \n",
" while vida > 0 and not tesoro_encontrado:\n",
" print(f\"Salud actual: {vida} HP | Objetos: {objetos}\")\n",
" camino = input(\"¿Qué camino eliges? (izquierda/derecha): \").lower()\n",
"\n",
" if camino == \"izquierda\":\n",
" \n",
" if random.random() <= 0.5:\n",
" print(\"¡Has encontrado una poción!\")\n",
" objetos.append(\"Poción\")\n",
" else:\n",
" print(\"¡Oh no! Has caído en una trampa.\")\n",
" vida -= 2\n",
" \n",
" elif camino == \"derecha\":\n",
" gano = encounter_ghost()\n",
" if gano:\n",
" print(\"Has encontrado una llave dorada.\")\n",
" objetos.append(\"llave\")\n",
" else:\n",
" vida -= 2\n",
" \n",
" else:\n",
" print(\"Entrada no válida. Inténtalo de nuevo.\")\n",
" \n",
"\n",
" if \"llave\" in objetos:\n",
" print(\"\\n¡Has usado la llave para abrir el cofre del tesoro!\")\n",
" tesoro_encontrado = True\n",
"\n",
" if vida <= 0:\n",
" print(\"\\nJuego terminado, has perdido todos tus puntos de salud.\")\n",
" elif tesoro_encontrado:\n",
" print(\"¡Felicidades! Has escapado de la mansión con el tesoro.\")\n",
"\n",
"if __name__ == \"__main__\":\n",
" run_mansion()\n"
]
},
{
Expand All @@ -141,16 +196,6 @@
"To run the game, simply call the run_mansion() function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"metadata": {},
"outputs": [],
"source": [
"run_mansion()"
]
},
{
"cell_type": "markdown",
"id": "88212f63-3bdb-479f-bf6c-4ecd0685d39a",
Expand All @@ -162,7 +207,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +221,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.1"
}
},
"nbformat": 4,
Expand Down