From eabefbfd7aab4d7dc8696438bb95f6e40ec5c5d0 Mon Sep 17 00:00:00 2001 From: Rina Bao Date: Thu, 20 Nov 2025 21:20:33 +0000 Subject: [PATCH] Solved lab --- .../lab-python-flow-control-checkpoint.ipynb | 94 +++++++++++++++++++ lab-python-flow-control.ipynb | 33 ++++++- 2 files changed, 126 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..52e57c5 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -0,0 +1,94 @@ +{ + "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": null, + "id": "977568c5-ce9f-4624-87d8-25f2670fc13b", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\",\"book\",\"keychain\"]\n", + "inventory = {}\n", + "for item in products:\n", + " x=int(input(f\"Please enter the number of {item}:\"))\n", + " inventory[item]=x\n", + "print(inventory)\n", + "\n", + "customer_orders=set()\n", + "order_end=False\n", + "while order_end!=True:\n", + " x=input(\"Please enter the name of the product:\")\n", + " customer_orders.add(x)\n", + " response=input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " if(response!=\"yes\"):\n", + " order_end=True\n", + "\n", + "for product in customer_orders:\n", + " if product in products:\n", + " inventory.update({product:inventory[product]-1})\n", + "\n", + "print(customer_orders)\n", + "print(inventory)" + ] + } + ], + "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.12.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..52e57c5 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,6 +37,37 @@ "\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": null, + "id": "977568c5-ce9f-4624-87d8-25f2670fc13b", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\",\"book\",\"keychain\"]\n", + "inventory = {}\n", + "for item in products:\n", + " x=int(input(f\"Please enter the number of {item}:\"))\n", + " inventory[item]=x\n", + "print(inventory)\n", + "\n", + "customer_orders=set()\n", + "order_end=False\n", + "while order_end!=True:\n", + " x=input(\"Please enter the name of the product:\")\n", + " customer_orders.add(x)\n", + " response=input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " if(response!=\"yes\"):\n", + " order_end=True\n", + "\n", + "for product in customer_orders:\n", + " if product in products:\n", + " inventory.update({product:inventory[product]-1})\n", + "\n", + "print(customer_orders)\n", + "print(inventory)" + ] } ], "metadata": { @@ -55,7 +86,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.4" } }, "nbformat": 4,