diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..b8b68c1e 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,11 +50,272 @@ "\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": [ + "Please enter a valid integer.\n", + " is not a valid choice and will not be added.\n" + ] + } + ], + "source": [ + "products_list = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "inventory = { }\n", + "for product in products_list:\n", + " while True:\n", + " quantity_input = input(f\"Enter the quantity for {product}: \") or '0'\n", + " try:\n", + " quantity = int(quantity_input)\n", + " inventory[product] = quantity\n", + " break\n", + " except ValueError:\n", + " print(\"Please enter a valid integer.\")\n", + "\n", + "customer_orders = set()\n", + "\n", + "while True:\n", + " product = input(\"Enter a product (choose from 't-shirt', 'mug', 'hat', 'book', 'keychain'): \").strip().lower()\n", + " \n", + " if product in products_list:\n", + " customer_orders.add(product)\n", + " print(f\"{product} has been added to the order.\")\n", + " else:\n", + " print(f\"{product} is not a valid choice and will not be added.\")\n", + " add_more = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " \n", + " if add_more != 'yes':\n", + " break\n", + "\n", + "print(\"products in order:\", customer_orders)\n", + "\n", + "total_ordered = len(customer_orders)\n", + "total_available = len(products_list)\n", + "percentage = (total_ordered / total_available) * 100\n", + "\n", + "order_status = (total_ordered, percentage)\n", + "\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", + "\n", + "for product in customer_orders:\n", + " if product in inventory and inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " print(f\"Successfully ordered {product}. Remaining: {inventory[product]}\")\n", + " \n", + "\n", + "print(\"\\nupdated inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + " \n", + " for product in products:\n", + "\n", + " while True:\n", + " quantity_input = input(f\"Enter the quantity for {product}: \") \n", + " try:\n", + " quantity = int(quantity_input)\n", + " inventory[product] = quantity\n", + " break\n", + " except ValueError:\n", + " print(\"Please enter a valid integer.\") \n", + " \n", + " return inventory\n" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " orders = set() # Local variable to collect customer orders\n", + " valid_products = {'t-shirt', 'mug', 'hat', 'book', 'keychain'}\n", + "\n", + " while True:\n", + " # Prompt the user for input\n", + " order = input(\"Enter a valid product name to add to your order: \").strip().lower()\n", + " \n", + " if order in valid_products:\n", + " orders.add(order) # Add order to set\n", + " print(f\"'{order}' has been added to your order.\")\n", + " else:\n", + " print(f\"Error: '{order}' is not a valid product.\")\n", + "\n", + " # After every attempt, ask if they want to continue adding\n", + " choice = \"\"\n", + " while choice not in {\"yes\", \"no\"}:\n", + " choice = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " if choice not in {\"yes\", \"no\"}:\n", + " print(\"Please enter 'yes' or 'no'.\")\n", + "\n", + " if choice == 'no':\n", + " break\n", + " return orders\n", + "\n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " \"\"\"\n", + " Iterates through customer orders and reduces inventory counts for valid items.\n", + " \"\"\"\n", + " for product in customer_orders:\n", + " # Check if product is a key in the inventory and has stock available\n", + " if product in inventory and inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " else:\n", + " # Handle cases where the item is out of stock or misspelled\n", + " print(f\"Error: '{product}' is not available or doesn't exist in inventory.\")\n", + " \n", + " return inventory\n" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " total_available_products = len(products)\n", + "\n", + " if total_products_ordered == 0:\n", + " return 0, 0.0\n", + "\n", + " percentage_unique_products_ordered = (total_products_ordered / total_available_products) * 100\n", + "\n", + " order_statistics = (total_products_ordered, percentage_unique_products_ordered)\n", + "\n", + " return order_statistics\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " print(\"\\norder statistics:\")\n", + " print(f\"total products ordered: {order_statistics[0]}\")\n", + " print(f\"percentage of unique products ordered: {order_statistics[1]:.2f}%\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(\"updated inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [], + "source": [ + "def execute_the_program():\n", + " \n", + " products_list = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + " inventory = initialize_inventory(products_list)\n", + " customer_orders = get_customer_orders()\n", + " updated_inventory = update_inventory(customer_orders, inventory)\n", + " order_statistics= calculate_order_statistics(customer_orders, products_list)\n", + " print_order_statistics(order_statistics)\n", + " print_updated_inventory(updated_inventory)\n", + " return\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "def manage_customer_orders():\n", + "\n", + " products_list = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + " inventory = initialize_inventory(products_list)\n", + " customer_orders = get_customer_orders()\n", + " updated_inventory = update_inventory(customer_orders, inventory)\n", + " order_statistics= calculate_order_statistics(customer_orders, products_list)\n", + " print_order_statistics(order_statistics)\n", + " print_updated_inventory(updated_inventory)\n", + " return\n", + "\n", + "\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -68,7 +329,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,