From 08ed4891a268dbd703ce05639175ec5dfb0a4f12 Mon Sep 17 00:00:00 2001 From: Sevgi Ozdemir Date: Tue, 1 Sep 2026 16:50:01 +0200 Subject: [PATCH] Solved lab --- lab-python-functions.ipynb | 179 ++++++++++++++++++++++++++++++++++++- 1 file changed, 177 insertions(+), 2 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..0f2ef47 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,11 +43,186 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "3ca8b829", + "metadata": {}, + "outputs": [], + "source": [ + "# 1. Initialize the inventory\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + "\n", + " for product in products:\n", + " quantity = int(input(f\"How many {product}s are available? \"))\n", + " inventory[product] = quantity\n", + "\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "eb99f071", + "metadata": {}, + "outputs": [], + "source": [ + "# 2. Collect customer orders\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + "\n", + " while True:\n", + " product = input(\n", + " \"Enter a product to order \"\n", + " \"(t-shirt, mug, hat, book, keychain): \"\n", + " ).lower()\n", + "\n", + " if product in products:\n", + " customer_orders.add(product)\n", + " print(f\"{product} added to the order.\")\n", + " else:\n", + " print(\"This product is not available.\")\n", + "\n", + " another_product = input(\n", + " \"Do you want to add another product? (yes/no): \"\n", + " ).lower()\n", + "\n", + " if another_product == \"no\":\n", + " break\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "733b0f38", + "metadata": {}, + "outputs": [], + "source": [ + "# 3. Update the inventory\n", + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " inventory[product] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "cfb7356b", + "metadata": {}, + "outputs": [], + "source": [ + "# 4. Calculate order statistics\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + "\n", + " percentage_ordered = (\n", + " total_products_ordered / len(products)\n", + " ) * 100\n", + "\n", + " return total_products_ordered, percentage_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "8a4f124c", + "metadata": {}, + "outputs": [], + "source": [ + "# 5. Print order statistics\n", + "def print_order_statistics(order_statistics):\n", + " total_products_ordered, percentage_ordered = order_statistics\n", + "\n", + " print(\"\\nOrder Statistics:\")\n", + " print(f\"Total Products Ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of Products Ordered: {percentage_ordered}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "a7520432", + "metadata": {}, + "outputs": [], + "source": [ + "# 6. Print updated inventory\n", + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + "\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "2f34bde9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mug added to the order.\n", + "hat added to the order.\n", + "book added to the order.\n", + "keychain added to the order.\n", + "\n", + "Products in customer order:\n", + "{'book', 'keychain', 'mug', 'hat'}\n", + "\n", + "Order Statistics:\n", + "Total Products Ordered: 4\n", + "Percentage of Products Ordered: 80.0%\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 20\n", + "mug: 9\n", + "hat: 9\n", + "book: 49\n", + "keychain: 99\n" + ] + } + ], + "source": [ + "# 7. Main program\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "\n", + "customer_orders = get_customer_orders()\n", + "\n", + "print(\"\\nProducts in customer order:\")\n", + "print(customer_orders)\n", + "\n", + "order_statistics = calculate_order_statistics(\n", + " customer_orders,\n", + " products\n", + ")\n", + "\n", + "print_order_statistics(order_statistics)\n", + "\n", + "update_inventory(customer_orders, inventory)\n", + "\n", + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "13e92be9", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -61,7 +236,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.6" } }, "nbformat": 4,