From e593c3c5e055f2c6c3cce0509e1f3d170ddba920 Mon Sep 17 00:00:00 2001 From: Bharat Singh Date: Fri, 18 Sep 2026 12:45:20 +0200 Subject: [PATCH] Solved Lab --- lab-python-flow-control.ipynb | 63 ---------------- lab_python_flow_control.ipynb | 135 ++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 63 deletions(-) delete mode 100644 lab-python-flow-control.ipynb create mode 100644 lab_python_flow_control.ipynb diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb deleted file mode 100644 index f4c7391..0000000 --- a/lab-python-flow-control.ipynb +++ /dev/null @@ -1,63 +0,0 @@ -{ - "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\")." - ] - } - ], - "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.9.13" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/lab_python_flow_control.ipynb b/lab_python_flow_control.ipynb new file mode 100644 index 0000000..b5d0df7 --- /dev/null +++ b/lab_python_flow_control.ipynb @@ -0,0 +1,135 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f", + "metadata": { + "tags": [], + "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f" + }, + "source": [ + "# Lab | Flow Control" + ] + }, + { + "cell_type": "markdown", + "id": "3851fcd1-cf98-4653-9c89-e003b7ec9400", + "metadata": { + "id": "3851fcd1-cf98-4653-9c89-e003b7ec9400" + }, + "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", + "source": [ + "inventory = {\"t-shirt\":10, \"mug\":20, \"hat\":30, \"book\":40, \"keychain\":10}\n", + "customer_orders=set()\n", + "\n", + "while True:\n", + " product = input(\"Enter the product you want to order:\")\n", + " customer_orders.add(product)\n", + " another_product = input(\"Do you want to order another product? Yes/ No:\")\n", + " if another_product.lower()==\"no\":\n", + " break\n", + "\n", + "print(customer_orders)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "qCRZmDBnFu-q", + "outputId": "b905f97f-5276-4c43-96f9-6a9df2108c91" + }, + "id": "qCRZmDBnFu-q", + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter the product you want to order:t-shirt\n", + "Do you want to order another product? Yes/ No:yes\n", + "Enter the product you want to order:book\n", + "Do you want to order another product? Yes/ No:yes\n", + "Enter the product you want to order:mug\n", + "Do you want to order another product? Yes/ No:no\n", + "{'book', 'mug', 't-shirt'}\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "for product in customer_orders:\n", + " inventory[product] -=1\n", + "print(inventory)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "qsxMQ8CqGHg5", + "outputId": "2f3b3e6c-952a-455c-d22e-b825d0da6732" + }, + "id": "qsxMQ8CqGHg5", + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "{'t-shirt': 9, 'mug': 19, 'hat': 30, 'book': 39, 'keychain': 10}\n" + ] + } + ] + } + ], + "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.9.13" + }, + "colab": { + "provenance": [] + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} \ No newline at end of file