diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..d1bf1fc 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,6 +43,397 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "252fad37-0bcc-4c75-b900-bf75a0bf4ed9", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " \"\"\"\n", + " list of products\n", + " using a loop and user input to create and return an inventory dictionary\n", + " \"\"\"\n", + " inventory = {}\n", + "\n", + " for product in products:\n", + " qty = int(input(f\"Enter initial quantity for {product}: \"))\n", + " inventory[product] = qty\n", + "\n", + " return inventory " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "f960f70d-01bc-4b57-be90-8674ab00cac1", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter initial quantity for tshirt: 5\n", + "Enter initial quantity for mug: 3\n", + "Enter initial quantity for hat: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'tshirt': 5, 'mug': 3, 'hat': 4}\n" + ] + } + ], + "source": [ + "products = [\"tshirt\", \"mug\", \"hat\"]\n", + "inventory = initialize_inventory(products)\n", + "\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "f7649f67-d461-47c7-bcb6-cf2bad0e6e8a", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " \"\"\"\n", + " Takes no parameters.\n", + " Using loop to prompt user for product names.\n", + " Return a set of customer_orders.\n", + " \"\"\"\n", + " customer_orders = set()\n", + "\n", + " while True:\n", + " product= input(\"Enter name of product to order: \").lower()\n", + " customer_orders.add(product)\n", + "\n", + " more = input(\"Add another product? (yes/no): \").lower()\n", + " if more != \"yes\":\n", + " break\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "16db7a1b-8f70-466e-b556-550774c12cb0", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter name of product to order: hat\n", + "Add another product? (yes/no): mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat'}\n" + ] + } + ], + "source": [ + "orders = get_customer_orders()\n", + "\n", + "print(orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "e0082a83-4550-417c-852f-6fad1488ff30", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " \"\"\"\n", + " Takes customer_orders (set) and inventory (dict).\n", + " Inventory update based on customer orders\n", + " Returns the updated inventory.\n", + " \"\"\"\n", + " for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + " else:\n", + " print(f\"Out of stock:'{product}' is not in the inventory. \")\n", + "\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "b070f400-5c25-49a1-898d-e11f8a7ec9a9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 4, 'mug': 2, 'hat': 4}\n" + ] + } + ], + "source": [ + "inventory = {\"t-shirt\":5, \"mug\":3, \"hat\":4}\n", + "customer_orders = {\"t-shirt\", \"mug\"}\n", + "\n", + "updated = update_inventory(customer_orders, inventory)\n", + "\n", + "print(updated)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "ae94c9d4-ac15-43dc-b144-78423084c77c", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " \"\"\"\n", + " Takes customer_orders (set) and products (list).\n", + " total_products_ordered\n", + " percentage_of_unique_products_ordered\n", + " returns as dictionary: order_statistics.\n", + " \"\"\"\n", + " total_products_ordered = len(customer_orders)\n", + "\n", + " valid_orders = [p for p in customer_orders if p in products]\n", + " unique_products_ordered = len(valid_orders)\n", + "\n", + " total_unique_products = len(products)\n", + "\n", + " if total_unique_products == 0:\n", + " percentage_of_unique_products_ordered = 0.0\n", + " else:\n", + " percentage_of_unique_products_ordered = (\n", + " unique_products_ordered / total_unique_products\n", + " ) * 100\n", + "\n", + " order_statistics = {\n", + " \"total_products_ordered\": total_products_ordered,\n", + " \"percentage_of_unique_products_ordered\": percentage_of_unique_products_ordered,\n", + " }\n", + "\n", + " return order_statistics" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "ee1e8049-2a4d-46e0-b68d-33043490572b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'total_products_ordered': 3, 'percentage_of_unique_products_ordered': 40.0}\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "customer_orders = {\"t-shirt\", \"mug\", \"unknown\"}\n", + "\n", + "stats = calculate_order_statistics(customer_orders, products)\n", + "\n", + "print(stats)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "b8a2e732-293d-487e-9d0b-c0bf7552b264", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " \"\"\"\n", + " Takes order_statistics (dict)\n", + " Prints the order statistics.\n", + " \"\"\"\n", + " print(\"\\n---Order Statistics---\")\n", + " print(\"Total products ordered:\", order_statistics[\"total_products_ordered\"])\n", + " print(\n", + " \"Percentage of unique products ordered:\",\n", + " f\"{order_statistics['percentage_of_unique_products_ordered']:.2f}%\",\n", + " )\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "63d347b2-e884-433c-9f7c-d691c4f905f2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "---Order Statistics---\n", + "Total products ordered: 3\n", + "Percentage of unique products ordered: 40.00%\n" + ] + } + ], + "source": [ + "stats = {\n", + " \"total_products_ordered\": 3,\n", + " \"percentage_of_unique_products_ordered\": 40.0,\n", + "}\n", + "\n", + "print_order_statistics(stats)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "28d78101-25dd-4d1f-b3fe-856470307dba", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " \"\"\"\n", + " Takes inventory (dict).\n", + " Prints the updated inventory\n", + " \"\"\"\n", + " print(\"\\n--- Updated Inventory ---\")\n", + " for product, qty in inventory.items():\n", + " print(f\"{product}: {qty}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "30273745-bcaa-407d-9f41-ca0d0c4852a7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "--- Updated Inventory ---\n", + "t-shirt: 4\n", + "mug: 2\n", + "hat: 4\n", + "book: 5\n", + "keychain: 6\n" + ] + } + ], + "source": [ + "inventory = {\"t-shirt\": 4, \"mug\": 2, \"hat\": 4, \"book\": 5, \"keychain\": 6}\n", + "\n", + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "149beebf-a75a-4ac8-95a2-f743996b7d43", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "=== Initialize Inventory ===\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter initial quantity for t-shirt: 5\n", + "Enter initial quantity for mug: 3\n", + "Enter initial quantity for hat: 4\n", + "Enter initial quantity for book: 2\n", + "Enter initial quantity for keychain: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "=== Get Customer Orders ===\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter name of product to order: hat\n", + "Add another product? (yes/no): yes\n", + "Enter name of product to order: keychain\n", + "Add another product? (yes/no): book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "---Order Statistics---\n", + "Total products ordered: 2\n", + "Percentage of unique products ordered: 40.00%\n", + "\n", + "--- Updated Inventory ---\n", + "t-shirt: 5\n", + "mug: 3\n", + "hat: 3\n", + "book: 2\n", + "keychain: 2\n" + ] + } + ], + "source": [ + "def main():\n", + "\n", + " products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + " print(\"=== Initialize Inventory ===\")\n", + " inventory = initialize_inventory(products)\n", + "\n", + " print(\"\\n=== Get Customer Orders ===\")\n", + " customer_orders = get_customer_orders()\n", + "\n", + " inventory = update_inventory(customer_orders, inventory)\n", + "\n", + " order_statistics = calculate_order_statistics(customer_orders, products)\n", + "\n", + " print_order_statistics(order_statistics)\n", + "\n", + " print_updated_inventory(inventory)\n", + "\n", + "\n", + "if __name__ == \"__main__\":\n", + " main()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "33b1aace-0f1f-4de4-a94f-65a3ea2bbcf8", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -61,7 +452,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.6" } }, "nbformat": 4,