Skip to content
Open
Show file tree
Hide file tree
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
Binary file added .DS_Store
Binary file not shown.
92 changes: 86 additions & 6 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -130,7 +130,70 @@
"\n",
" \"\"\"\n",
" \n",
" # your code goes here"
" # your code goes here\n",
" import random\n",
"\n",
"def encounter_ghost():\n",
" \"\"\"\n",
" Simula el encuentro con un fantasma.\n",
" Devuelve True si el aventurero gana, False si pierde.\n",
" \"\"\"\n",
" print(\"👻 You encounter a ghost!\")\n",
" roll = random.randint(1, 10) # número aleatorio entre 1 y 10\n",
"\n",
" if roll <= 5:\n",
" print(\"✅ You defeated the ghost!\")\n",
" return True\n",
" else:\n",
" print(\"❌ You lost the battle...\")\n",
" return False\n",
"\n",
"\n",
"def run_mansion():\n",
" \"\"\"\n",
" Juego principal: Haunted Mansion.\n",
" El aventurero empieza con 10 puntos de vida y sin objetos.\n",
" \"\"\"\n",
" health = 10\n",
" items = []\n",
"\n",
" print(\"🏰 Welcome to the Haunted Mansion!\")\n",
" print(f\"You start with {health} health points.\")\n",
"\n",
" while health > 0:\n",
" choice = input(\"\\nDo you go 'left' or 'right'? \").lower()\n",
"\n",
" if choice == \"left\":\n",
" # Evento aleatorio: poción o trampa\n",
" if random.choice([True, False]):\n",
" print(\"🍵 You found a potion! Health +2.\")\n",
" health += 2\n",
" items.append(\"potion\")\n",
" else:\n",
" print(\"⚠️ You fell into a trap! Health -2.\")\n",
" health -= 2\n",
"\n",
" elif choice == \"right\":\n",
" won = encounter_ghost()\n",
" if won:\n",
" print(\"🗝 You found a key!\")\n",
" items.append(\"key\")\n",
" else:\n",
" health -= 2\n",
" print(f\"💔 You lost 2 health. Remaining: {health}\")\n",
"\n",
" else:\n",
" print(\"❓ Invalid choice, try again.\")\n",
" continue\n",
"\n",
" # Condiciones de fin\n",
" if health <= 0:\n",
" print(\"☠️ Game over, you lost all your health points.\")\n",
" break\n",
"\n",
" if \"key\" in items:\n",
" print(\"🎉 You unlocked the door and found the Treasure! Congratulations!\")\n",
" break\n"
]
},
{
Expand All @@ -143,10 +206,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"🏰 Welcome to the Haunted Mansion!\n",
"You start with 10 health points.\n",
"🍵 You found a potion! Health +2.\n",
"❓ Invalid choice, try again.\n",
"🍵 You found a potion! Health +2.\n",
"⚠️ You fell into a trap! Health -2.\n",
"👻 You encounter a ghost!\n",
"✅ You defeated the ghost!\n",
"🗝 You found a key!\n",
"🎉 You unlocked the door and found the Treasure! Congratulations!\n"
]
}
],
"source": [
"run_mansion()"
]
Expand All @@ -162,7 +242,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +256,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down