From 3a591e213d3ac0fc6e185ba133e369775de7cc6c Mon Sep 17 00:00:00 2001 From: Charul Mathur Date: Mon, 24 Nov 2025 15:43:37 +0100 Subject: [PATCH] Day2 Lab1 solved --- .../lab-python-flow-control-checkpoint.ipynb | 280 ++++++++++++++++++ lab-python-flow-control.ipynb | 219 +++++++++++++- 2 files changed, 498 insertions(+), 1 deletion(-) create mode 100644 .ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb diff --git a/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb new file mode 100644 index 0000000..1f8f06e --- /dev/null +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -0,0 +1,280 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f", + "metadata": { + "tags": [] + }, + "source": [ + "# Lab | Flow Control" + ] + }, + { + "cell_type": "markdown", + "id": "3851fcd1-cf98-4653-9c89-e003b7ec9400", + "metadata": {}, + "source": [ + "## Exercise: Managing Customer Orders Optimized\n", + "\n", + "In the last lab, you were starting an online store that sells various products. To ensure smooth operations, you developed a program that manages customer orders and inventory.\n", + "\n", + "You did so without using flow control. Let's go a step further and improve this code.\n", + "\n", + "Follow the steps below to complete the exercise:\n", + "\n", + "1. Look at your code from the lab data structures, and improve repeated code with loops.\n", + "\n", + "2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:\n", + " \n", + " a. Prompt the user to enter the name of a product that a customer wants to order.\n", + " \n", + " b. Add the product name to the \"customer_orders\" set.\n", + " \n", + " c. Ask the user if they want to add another product (yes/no).\n", + " \n", + " d. Continue the loop until the user does not want to add another product.\n", + "\n", + "3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "fa0f21e3-48c8-4d6f-83c9-8fd968140b54", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 1: Define the product list\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "d7ed8711-58e7-46b9-8750-0eda5a574ffb", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 2: Create an empty dictionary\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "126124a1-1636-4f01-988a-6014ce17fa4e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the quantity for each product: \n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Quantity of t-shirt: 8\n", + "Quantity of mug: 7\n", + "Quantity of hat: 4\n", + "Quantity of book: 5\n", + "Quantity of keychain: 6\n" + ] + } + ], + "source": [ + "# Step 3: Ask the user for the quantity of each product\n", + "print(\"Enter the quantity for each product: \")\n", + "for item in products:\n", + " quantity = int(input(f\"Quantity of {item}: \"))\n", + " inventory[item] = quantity" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "2f9794a8-db9a-4807-a80f-56f50ef88259", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 4: Create an empty set\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "018ea7ac-0c16-49c6-8c53-1d23b6a5af04", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the product that customer wants to order: mug\n", + "Do you want to add another product? (yes/no): no\n" + ] + } + ], + "source": [ + "#Step 5: Ask the user to enter the name of a product that a customer wants to order.\n", + "\n", + "print(\"Available products: \", products)\n", + "\n", + "while True:\n", + " product = input(\"Enter the product that customer wants to order: \").strip().lower()\n", + "\n", + " while product not in products:\n", + " print(\"Invalid Product. Choose from: \", products)\n", + " product = input(\"Enter a valid product: \").strip().lower()\n", + "\n", + " customer_orders.add(product)\n", + "\n", + " another_product = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " \n", + " if another_product != \"yes\":\n", + " break" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "66d33615-de38-48d8-b669-ef0d84a8a2f8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer Orders: {'mug'}\n" + ] + } + ], + "source": [ + "# Step 6: Print ordered products\n", + "\n", + "print(\"Customer Orders: \", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "ead4276b-1c70-47b5-a956-69243b376061", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 7: Calculate order statistics\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "\n", + "# Storing in tuple\n", + "order_status = (total_products_ordered, percentage_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "e9f30394-8f9d-4ff5-a4ef-85e2faefd9e9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order statistics: \n", + "Total products ordered: 1\n", + "Percentage of products ordered: 20.00%\n" + ] + } + ], + "source": [ + "# Step 8: Print the statistics\n", + "\n", + "print(\"Order statistics: \")\n", + "print(\"Total products ordered: \", order_status[0])\n", + "print(\"Percentage of products ordered: \", f\"{order_status[1]:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "e9ed43d0-ef15-4c1e-96ff-532c90837782", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 9: Update the inventory only for the ordered items\n", + "\n", + "for item in customer_orders:\n", + " if inventory[item] > 0:\n", + " inventory[item] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "3621dce5-f712-4d1a-8aae-df2bc8d15eda", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated inventory: \n", + "t-shirt: 8\n", + "mug: 6\n", + "hat: 4\n", + "book: 5\n", + "keychain: 6\n" + ] + } + ], + "source": [ + "# Step 10:Updated inventory\n", + "\n", + "print(\"Updated inventory: \")\n", + "for item, quantity in inventory.items():\n", + " print(f\"{item}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0bfe2cee-f14d-4eb0-a695-e5351f3e17a4", + "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.13.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..1f8f06e 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,6 +37,223 @@ "\n", "3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")." ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "fa0f21e3-48c8-4d6f-83c9-8fd968140b54", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 1: Define the product list\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "d7ed8711-58e7-46b9-8750-0eda5a574ffb", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 2: Create an empty dictionary\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "126124a1-1636-4f01-988a-6014ce17fa4e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the quantity for each product: \n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Quantity of t-shirt: 8\n", + "Quantity of mug: 7\n", + "Quantity of hat: 4\n", + "Quantity of book: 5\n", + "Quantity of keychain: 6\n" + ] + } + ], + "source": [ + "# Step 3: Ask the user for the quantity of each product\n", + "print(\"Enter the quantity for each product: \")\n", + "for item in products:\n", + " quantity = int(input(f\"Quantity of {item}: \"))\n", + " inventory[item] = quantity" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "2f9794a8-db9a-4807-a80f-56f50ef88259", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 4: Create an empty set\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "018ea7ac-0c16-49c6-8c53-1d23b6a5af04", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the product that customer wants to order: mug\n", + "Do you want to add another product? (yes/no): no\n" + ] + } + ], + "source": [ + "#Step 5: Ask the user to enter the name of a product that a customer wants to order.\n", + "\n", + "print(\"Available products: \", products)\n", + "\n", + "while True:\n", + " product = input(\"Enter the product that customer wants to order: \").strip().lower()\n", + "\n", + " while product not in products:\n", + " print(\"Invalid Product. Choose from: \", products)\n", + " product = input(\"Enter a valid product: \").strip().lower()\n", + "\n", + " customer_orders.add(product)\n", + "\n", + " another_product = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " \n", + " if another_product != \"yes\":\n", + " break" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "66d33615-de38-48d8-b669-ef0d84a8a2f8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer Orders: {'mug'}\n" + ] + } + ], + "source": [ + "# Step 6: Print ordered products\n", + "\n", + "print(\"Customer Orders: \", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "ead4276b-1c70-47b5-a956-69243b376061", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 7: Calculate order statistics\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "\n", + "# Storing in tuple\n", + "order_status = (total_products_ordered, percentage_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "e9f30394-8f9d-4ff5-a4ef-85e2faefd9e9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order statistics: \n", + "Total products ordered: 1\n", + "Percentage of products ordered: 20.00%\n" + ] + } + ], + "source": [ + "# Step 8: Print the statistics\n", + "\n", + "print(\"Order statistics: \")\n", + "print(\"Total products ordered: \", order_status[0])\n", + "print(\"Percentage of products ordered: \", f\"{order_status[1]:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "e9ed43d0-ef15-4c1e-96ff-532c90837782", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 9: Update the inventory only for the ordered items\n", + "\n", + "for item in customer_orders:\n", + " if inventory[item] > 0:\n", + " inventory[item] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "3621dce5-f712-4d1a-8aae-df2bc8d15eda", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated inventory: \n", + "t-shirt: 8\n", + "mug: 6\n", + "hat: 4\n", + "book: 5\n", + "keychain: 6\n" + ] + } + ], + "source": [ + "# Step 10:Updated inventory\n", + "\n", + "print(\"Updated inventory: \")\n", + "for item, quantity in inventory.items():\n", + " print(f\"{item}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0bfe2cee-f14d-4eb0-a695-e5351f3e17a4", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -55,7 +272,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,