diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..01f075dd 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,6 +50,175 @@ "\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": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input the quantity of product t-shirt: 10\n", + "Please input the quantity of product mug: 10\n", + "Please input the quantity of product hat: 10\n", + "Please input the quantity of product book: 10\n", + "Please input the quantity of product keychain: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + } + ], + "source": [ + "# create a product list, inventory dictionary\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = {} \n", + "\n", + "for p in products:\n", + " quantity = int(input(f\"Please input the quantity of product {p}:\"))\n", + " inventory[p] = quantity\n", + " \n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } + }, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input the name of product that a customer wants to order: mug\n", + "Please input the name of product that a customer wants to order: hat\n", + "Please input the name of product that a customer wants to order: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'mug', 'book'}\n" + ] + } + ], + "source": [ + "# create a customer order set\n", + "\n", + "customer_orders = set()\n", + " \n", + "for i in range(3):\n", + " product = input(\"Please input the name of product that a customer wants to order:\")\n", + " customer_orders.add(product)\n", + "\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# ---------------------------------------only execute below codes---------------------------------------------------------------------------------------" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + " Total Products Ordered: 3\n", + " Percentage of Products Ordered: 60% \n" + ] + } + ], + "source": [ + "# create 0rder_Stats tuple\n", + "\n", + "Total_Products_Ordered = len(customer_orders)\n", + "Percentage_of_Products_Ordered = (Total_Products_Ordered/len(inventory))\n", + "\n", + "order_status = (Total_Products_Ordered,Percentage_of_Products_Ordered)\n", + "\n", + "print(f\"Order Statistics:\\n Total Products Ordered: {order_status[0]}\\n Percentage of Products Ordered: {order_status[1]*100:.0f}% \") \n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 9, 'hat': 9, 'book': 9, 'keychain': 10}\n" + ] + } + ], + "source": [ + "# Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n", + "\n", + "for o in customer_orders:\n", + " inventory[o] -= 1\n", + " \n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "updated inventory overview:\n", + "\n", + "t-shirt 10\n", + "mug 9\n", + "hat 9\n", + "book 9\n", + "keychain 10\n" + ] + } + ], + "source": [ + "# Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "\n", + "print(f\"updated inventory overview:\\n\")\n", + "\n", + "for k,v in inventory.items(): \n", + " print(f\"{k} {v}\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -68,7 +237,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.10.10" } }, "nbformat": 4,