diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..01ac8fdb 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -21,6 +21,7 @@ "\n", "1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", "\n", + "\n", "2. Create an empty dictionary called `inventory`.\n", "\n", "3. Ask the user to input the quantity of each product available in the inventory. Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values.\n", @@ -50,11 +51,154 @@ "\n", "Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. " ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 21, 'mug': 33, 'hat': 45, 'book': 34, 'keychain': 54}\n" + ] + } + ], + "source": [ + "products=[\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "inventory={}\n", + "for p in products:\n", + " quantity= int(input(f\"how many units of {p} is in the inventory? \")) \n", + " if quantity == \"\":\n", + " quantity=0\n", + " else:\n", + " quantity =int(quantity)\n", + " \n", + " inventory[p]=quantity\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Choose 3 products from the list: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + } + ], + "source": [ + "products=[\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "costumer_orders= set()\n", + "print(\"Choose 3 products from the list:\", products)\n", + "for i in range(3):\n", + " while True:\n", + " item = input(f\"Product {i+1}: \").strip().lower()\n", + " if item in products:\n", + " costumer_orders.add (item)\n", + " break\n", + " else:\n", + " print=(\"Invalid product. Choose from:\", products)\n", + "\n", + "\n", + "print(\"\\nProducts Ordered:\", costumer_orders)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " \n" + ] + } + ], + "source": [ + "import builtins\n", + "builtins.print(type(print), print) " + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 1.60%\n" + ] + } + ], + "source": [ + "products=[\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "inventory={'t-shirt': 21, 'mug': 33, 'hat': 45, 'book': 34, 'keychain': 54}\n", + "costumer_orders= set()\n", + "costumer_orders.update([\"t-shirt\",\"mug\",\"hat\"])\n", + "total_ordered = len(costumer_orders)\n", + "total_inventory = sum(inventory.values())\n", + "if total_inventory > 0:\n", + " percentage_ordered = (total_ordered / total_inventory) * 100\n", + "else:\n", + " percentage_ordered = 0\n", + "print(f\"Total Products Ordered: {total_ordered}\")\n", + "print(f\"Percentage of Products Ordered: {percentage_ordered:.2f}%\")\n", + "\n", + "\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Inventory:\n", + "t-shirt: 20\n", + "mug: 32\n", + "hat: 44\n", + "book: 34\n", + "keychain: 54\n" + ] + } + ], + "source": [ + "\n", + "for item in costumer_orders:\n", + " if inventory.get(item, 0) > 0:\n", + " inventory[item] -= 1\n", + "print(\"\\nUpdated Inventory:\")\n", + "for p in products:\n", + " print(f\"{p}: {inventory.get(p, 0)}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -68,7 +212,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,