diff --git a/lab-python-data-structure.ipynb b/lab-python-data-structure.ipynb new file mode 100644 index 00000000..99b6939d --- /dev/null +++ b/lab-python-data-structure.ipynb @@ -0,0 +1,327 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "7c97e3d6-36e4-4374-8b26-8a294efed761", + "metadata": {}, + "source": [ + "## Define product lists" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "d97ce447-fa8f-4d43-9bb6-9a3e39dfba22", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "2b5122b4-d9fb-428d-abef-ce974b4de44f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter inventory quantity for each product:\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "t-shirt: 1\n", + "mug: 1\n", + "hat: 1\n", + "book: 1\n", + "keychain: 1\n" + ] + } + ], + "source": [ + "# Step 3: Ask user to input quantity for each product\n", + "print(\"Enter inventory quantity for each product:\")\n", + "for product in products:\n", + " while True:\n", + " try:\n", + " qty = int(input(f\"{product}: \"))\n", + " if qty < 0:\n", + " print(\"Quantity cannot be negative. Try again.\")\n", + " continue\n", + " inventory[product] = qty\n", + " break\n", + " except ValueError:\n", + " print(\"Please enter a valid number.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "34dc00f2-a657-4a8f-aec7-a34bf287b529", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 4: Create an empty set for customer orders\n", + "customer_orders = set()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "f175c366-f264-46a4-8ec7-f9eb1447fd44", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Enter 3 products for the customer order:\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Product 1: 1,3,4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product. Choose from the available products.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Product 1: t-shirt, mug, book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product. Choose from the available products.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Product 1: mug, hat, book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product. Choose from the available products.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Product 1: mut:1, t-shirt:1, book:1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product. Choose from the available products.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Product 1: mug:1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product. Choose from the available products.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Product 1: hat:1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product. Choose from the available products.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Product 1: hat\n", + "Product 2: mug\n", + "Product 3: book\n" + ] + } + ], + "source": [ + "# Step 5: Ask user to input 3 products for the order\n", + "print(\"\\nEnter 3 products for the customer order:\")\n", + "while len(customer_orders) < 3:\n", + " order = input(f\"Product {len(customer_orders)+1}: \").lower()\n", + " if order not in products:\n", + " print(\"Invalid product. Choose from the available products.\")\n", + " elif order in customer_orders:\n", + " print(\"You already added this product.\")\n", + " else:\n", + " customer_orders.add(order)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "4eb2c792-3604-47b5-bd17-524e4c6b8853", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Customer Orders:\n", + "hat\n", + "mug\n", + "book\n" + ] + } + ], + "source": [ + "# Step 6: Print the products in the customer_orders set\n", + "print(\"\\nCustomer Orders:\")\n", + "for item in customer_orders:\n", + " print(item)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "8d9739c5-33b7-4ff8-88d9-6ef61be5c44b", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 7: Calculate order statistics\n", + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "order_status = (total_products_ordered, percentage_ordered)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "571a9dd7-c327-4398-9502-638363b2dc3d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.00%\n" + ] + } + ], + "source": [ + "# Step 8: Print order statistics\n", + "print(\"\\nOrder Statistics:\")\n", + "print(f\"Total Products Ordered: {order_status[0]}\")\n", + "print(f\"Percentage of Products Ordered: {order_status[1]:.2f}%\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "8d6eba54-4f3a-4766-af29-426b337a0ea5", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 9: Update the inventory (subtract 1 for each ordered product)\n", + "for item in customer_orders:\n", + " if inventory[item] > 0:\n", + " inventory[item] -= 1\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "c4d7bb77-3762-4d82-9ffd-da79ff2f37b2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Inventory:\n", + "t-shirt: 1\n", + "mug: 0\n", + "hat: 0\n", + "book: 0\n", + "keychain: 1\n" + ] + } + ], + "source": [ + "\n", + "# Step 10: Print updated inventory\n", + "print(\"\\nUpdated Inventory:\")\n", + "for product, qty in inventory.items():\n", + " print(f\"{product}: {qty}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5e8de26c-e1c2-4c17-a6b1-8c4cd534a01e", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}