From 2b3e778e16a97e19cc6643782e0977a0e13eb2c0 Mon Sep 17 00:00:00 2001 From: NoidFrancis Date: Wed, 6 Aug 2025 10:10:04 +0200 Subject: [PATCH] Week1 Lab2 Done --- lab-python-flow-control.ipynb | 171 +++++++++++++++++++++++++++++++++- 1 file changed, 169 insertions(+), 2 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..d27f03e 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -10,6 +10,173 @@ "# Lab | Flow Control" ] }, + { + "cell_type": "code", + "execution_count": 1, + "id": "8619afc7", + "metadata": {}, + "outputs": [], + "source": [ + "#1 Define the product list\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "3adb391a", + "metadata": {}, + "outputs": [], + "source": [ + "#2 Create an empty inventory dictionary\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "3dad3e35", + "metadata": {}, + "outputs": [], + "source": [ + "#3 Input quantity for each product\n", + "for product in products:\n", + " quantity = int(input(f\"Enter quantity available for {product}: \"))\n", + " inventory[product] = quantity" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "48acd4f7", + "metadata": {}, + "outputs": [], + "source": [ + " #4 Create an empty set for customer orders\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "a49c9c8a", + "metadata": {}, + "outputs": [], + "source": [ + "#5 Ask for product orders (fixed maximum number of attempts, e.g., 10)\n", + "for _ in range(10): # You can change 10 to a reasonable number\n", + " order = input(\"Enter a product to order (t-shirt, mug, hat, book, keychain): \").strip().lower()\n", + "\n", + " if order in products:\n", + " customer_orders.add(order)\n", + " else:\n", + " print(\"Invalid product. Try again.\")\n", + " continue\n", + "\n", + " add_more = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " if add_more != \"yes\":\n", + " break" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "dda38058", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Customer Orders: {'book', 'mug', 'hat'}\n" + ] + } + ], + "source": [ + "#6 Print the customer orders\n", + "print(\"\\nCustomer Orders:\", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "b31ea90f", + "metadata": {}, + "outputs": [], + "source": [ + "#7 Calculate 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)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "4723460e", + "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": [ + "#8 Print 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}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "0fecd67c", + "metadata": {}, + "outputs": [], + "source": [ + "#9 Update inventory only for ordered items\n", + "for product in customer_orders:\n", + " if inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " else:\n", + " print(f\"Warning: {product} is out of stock!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "c5dae8f3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Inventory:\n", + "t-shirt: 9\n", + "mug: 7\n", + "hat: 6\n", + "book: 5\n", + "keychain: 5\n" + ] + } + ], + "source": [ + "#10 Print updated inventory\n", + "print(\"\\nUpdated Inventory:\")\n", + "for product in inventory:\n", + " print(f\"{product}: {inventory[product]}\")" + ] + }, { "cell_type": "markdown", "id": "3851fcd1-cf98-4653-9c89-e003b7ec9400", @@ -41,7 +208,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -55,7 +222,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,