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
117 changes: 87 additions & 30 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,25 @@
" Good luck!"
]
},
{
"cell_type": "markdown",
"id": "9e13a33c-38e5-44b3-bd1b-9a642c962c89",
"metadata": {},
"source": [
"To run the game, simply call the run_mansion() function:"
]
},
{
"cell_type": "markdown",
"id": "88212f63-3bdb-479f-bf6c-4ecd0685d39a",
"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. "
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "499552c8-9e30-46e1-a706-4ac5dc64670e",
"metadata": {},
"outputs": [],
Expand All @@ -91,23 +107,24 @@
" 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"
" ghost_battle = (False, True)\n",
" import random\n",
" outcome = random.randint(1, 10)\n",
" if outcome <= 5:\n",
" return ghost_battle[1]\n",
" else:\n",
" return ghost_battle[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"metadata": {},
"outputs": [],
"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",
Expand All @@ -127,42 +144,82 @@
" 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",
" \"\"\"\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:"
" items = {\"potions\": 0, \"keys\": 0, \"weapons\": 0}\n",
" health_points = 10\n",
" import random\n",
" ghost_battle = (False, True)\n",
" print(\"Choose a path to continue: left or right?\")\n",
" choice = input(\"Choose a path to continue: left or right?\").strip().lower()\n",
" if health_points > 0:\n",
" while health_points > 0:\n",
" if choice in [\"left\", \"right\"]:\n",
" if choice == \"left\":\n",
" left_event = random.randint(1, 2)\n",
" if left_event == 1:\n",
" print(\"You found a health potion! It is added to your inventory.\")\n",
" items[\"potions\"] += 1\n",
" print(f\"You now have {items['potions']} potions in your inventory.\")\n",
" print(\"Choose a path to continue: left or right?\")\n",
" choice = input(\"Choose a path to continue: left or right?\").strip().lower()\n",
" else:\n",
" print(\"You fell into a trap and lost 2 health points.\")\n",
" health_points -= 2\n",
" if health_points <= 0:\n",
" print(\"You have no health points left. Game Over. Run the game again to try to win!\")\n",
" print(f\"You finished the game with the following potions in your inventory: {items['potions']}\")\n",
" items.clear()\n",
" break\n",
" else:\n",
" print(f\"You now have {health_points} health points.\")\n",
" print(\"Choose a path to continue: left or right?\")\n",
" choice = input(\"Choose a path to continue: left or right?\").strip().lower()\n",
" else:\n",
" print(\"You encountered a ghost!\")\n",
" print(\"Press any key whenever you are ready for the battle!\")\n",
" input(\"Press any key whenever you are ready for the battle!\").strip().lower()\n",
" ghost_battle = encounter_ghost()\n",
" if ghost_battle == False:\n",
" health_points -= 2\n",
" if health_points <= 0:\n",
" print(\"You have no health points left. Game Over. Run the game again to try to win!\")\n",
" print(f\"You finished the game with the following potions in your inventory: {items['potions']}\")\n",
" items.clear()\n",
" break\n",
" else:\n",
" print(\"You lost the battle and lost 2 health points.\")\n",
" print(f\"You now have {health_points} health points.\")\n",
" print(\"Choose a path to continue: left or right?\")\n",
" choice = input(\"Choose a path to continue: left or right?\").strip().lower()\n",
" else:\n",
" print(\"You won the battle against the ghost and found a key, which is now stored in your inventory!\")\n",
" items[\"keys\"] += 1\n",
" if items[\"keys\"] >= 1:\n",
" print(\"You unlocked the door and found the Treasure! Congratulations!\")\n",
" print(f\"You finished the game with {health_points} health points left and the following potions in your inventory: {items['potions']}\")\n",
" items.clear()\n",
" break\n",
" else:\n",
" print(\"Invalid choice. Please choose left or right.\")\n",
" choice = input(\"Choose a path to continue: left or right?\").strip().lower()\n",
" else:\n",
" print(\"You have no health points left. Game Over. Run the game again to try to win!\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"id": "35dcc1e6",
"metadata": {},
"outputs": [],
"source": [
"run_mansion()"
]
},
{
"cell_type": "markdown",
"id": "88212f63-3bdb-479f-bf6c-4ecd0685d39a",
"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. "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -176,7 +233,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.9"
}
},
"nbformat": 4,
Expand Down