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
143 changes: 132 additions & 11 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@
" Good luck!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7213c6be",
"metadata": {},
"outputs": [],
"source": [
"inventory=[\"knife\",\"flower\",\"shield\", \"axe\"]\n",
"\n",
"INIT_GAME_STATE = {\n",
" \"health\": 10,\n",
" \"trasure_key\": False\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -93,23 +108,104 @@
" \"\"\"\n",
" print(\"You encounter a ghost!\")\n",
" \n",
" # your code goes here"
" # your code goes here\n",
"\n",
"import random # cuidado, es random, no \"ramdom\"\n",
"\n",
"def run_mansion():\n",
" print(\"🏰 You are back in the mansion...\")\n",
"\n",
"def encounter_ghost():\n",
" print(\"A ghost appeared! 👻 Buuuuuu\")\n",
" action = input(\"What would you like to do, run or fight?: \").strip().lower()\n",
"\n",
" if action == \"fight\":\n",
" if \"axe\" in inventory:\n",
" # 80% de probabilidad de ganar\n",
" if random.random() < 0.8:\n",
" print(\"⚔️ You swing your axe and kill the ghost.\")\n",
" print(\"You foun a Key, Lets unlock the treasure\")\n",
" INIT_GAME_STATE[\"treasure_key\"] = True\n",
" def treasure():\n",
" \n",
"\n",
" else:\n",
" print(\"You hesitate... and the ghost takes advantage. -8 damage.\")\n",
" INIT_GAME_STATE[\"health\"] -= 8\n",
" if INIT_GAME_STATE[\"health\"] < 1:\n",
" print(\"☠️ You die.\")\n",
" else:\n",
" run_mansion()\n",
" else:\n",
" print(\"You try to fight with your bare hands... -8 damage.\")\n",
" INIT_GAME_STATE[\"health\"] -= 8\n",
" if INIT_GAME_STATE[\"health\"] < 1:\n",
" print(\"☠️ You die.\")\n",
" else:\n",
" run_mansion()\n",
"\n",
" elif action == \"run\":\n",
" print(\"🏃 You run away as fast as you can! The ghost disappears...\")\n",
" run_mansion()\n",
"\n",
" else:\n",
" print(\"You hesitate... and the ghost takes advantage. -8 damage.\")\n",
" INIT_GAME_STATE[\"health\"] -= 8\n",
" if INIT_GAME_STATE[\"health\"] < 1:\n",
" print(\"☠️ You die.\")\n",
" else:\n",
" run_mansion()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'\\nSimulates 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\\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\\n'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# main function for the game\n",
"import random\n",
"\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",
" direction = input(\"What direction would you take? Type 'left' or 'right'?: \").strip().lower()\n",
"\n",
" if direction == \"right\":\n",
" encounter_ghost() # Asumo que ya la tienes definida\n",
"\n",
" elif direction == \"left\":\n",
" # 50% de caer en trampa vs encontrar poción\n",
" if random.random() < 0.5: # ← 50/50 real\n",
" print(\"You went left. It's very quiet and you fall into a trap. -2 HEALTH POINTS\")\n",
" INIT_GAME_STATE[\"health\"] -= 2\n",
" if INIT_GAME_STATE[\"health\"] <= 0:\n",
" print(\"You die\")\n",
" else:\n",
" print(f\"You survive the trap. Health is now {INIT_GAME_STATE['health']}.\")\n",
" run_mansion()\n",
" else:\n",
" print(\"You went left. It's very quiet... You found a potion!\")\n",
" inventory.append(\"potion\") # ← en lugar de inventory.__add__(potion)\n",
"\n",
" else:\n",
" print(\"Wrong value, try again.\")\n",
" run_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",
" 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",
Expand All @@ -128,11 +224,28 @@
" Treasure! Congratulations!\".\n",
" If they don't have the key, they are prompted to find it from the beginning.\n",
"\n",
" \"\"\"\n",
"\"\"\"\n",
" \n",
" # your code goes here"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4b36ca28",
"metadata": {},
"outputs": [],
"source": [
"def treasure():\n",
" if INIT_GAME_STATE[\"treasure_key\"] = True\n",
" print \"You got all the gold\"\n",
" else \n",
" print \"You need the key to unlock the treaure\"\n",
" def run_mansion():\n",
" \n",
" "
]
},
{
"cell_type": "markdown",
"id": "9e13a33c-38e5-44b3-bd1b-9a642c962c89",
Expand All @@ -143,10 +256,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"🏰 You are back in the mansion...\n"
]
}
],
"source": [
"run_mansion()"
]
Expand All @@ -162,7 +283,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +297,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.7"
}
},
"nbformat": 4,
Expand Down