diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7905339..86a162f 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -75,6 +75,16 @@ " Good luck!" ] }, + { + "cell_type": "code", + "execution_count": 13, + "id": "119ba7a3", + "metadata": {}, + "outputs": [], + "source": [ + "import random" + ] + }, { "cell_type": "code", "execution_count": null, @@ -93,7 +103,15 @@ " \"\"\"\n", " print(\"You encounter a ghost!\")\n", " \n", - " # your code goes here" + " # your code goes here\n", + " event = random.randint(1,10)\n", + " if event <= 5:\n", + " print(\"You defeated the ghost!\")\n", + " return True\n", + " else:\n", + " print(\"You lost the battle...\")\n", + " return False\n", + " " ] }, { @@ -130,7 +148,49 @@ "\n", " \"\"\"\n", " \n", - " # your code goes here" + " # your code goes here\n", + " health = 10 \n", + " items_adventurer = []\n", + "\n", + " while health > 0:\n", + " choice = input(\"Choose between two paths: (left or right)\").lower()\n", + " \n", + " if choice == 'left':\n", + " event1 = random.choice([\"potion\", \"trap\"])\n", + "\n", + " if event1 == 'potion':\n", + " print(\"You found a potion!\")\n", + " items_adventurer.append('potion')\n", + " else:\n", + " print(\"You feel into a trap!, You lose 2 points of health)\")\n", + " health -=2\n", + "\n", + " elif choice == 'right':\n", + " event2 = encounter_ghost()\n", + "\n", + " if event2:\n", + " print(\"You defeated the ghost and found a key!\")\n", + " items_adventurer.append(\"key\")\n", + " else:\n", + " print(\"The ghost won!, You lose 2 points of health =()\")\n", + " health -= 2\n", + "\n", + " else:\n", + " print(\"Invalid choice. Try again\")\n", + " continue\n", + "\n", + " #Health\n", + " if health <= 0:\n", + " print(\"Game over, you lost all your health points.\")\n", + " return \n", + " \n", + " #The player has the key\n", + " if \"key\" in items_adventurer:\n", + " print(\"You unlocked the door and found the Treasure! Congratulations!\")\n", + " return \n", + " else:\n", + " print(\"You don't have the key. Try again from the beginning\")\n", + "\n" ] }, { @@ -143,10 +203,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Haunted Mansion!\n" + ] + } + ], "source": [ "run_mansion()" ] @@ -158,11 +226,17 @@ "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": "markdown", + "id": "96e0af2d", + "metadata": {}, + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -176,7 +250,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.10.2" } }, "nbformat": 4,