From 870e2b5558f9870849ca1973fb2cca6afc9159f7 Mon Sep 17 00:00:00 2001 From: AnaP2020 Date: Sun, 10 May 2026 17:43:15 +0100 Subject: [PATCH] Lab Done --- lab-python-flow-control.ipynb | 87 +++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 lab-python-flow-control.ipynb diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb new file mode 100644 index 0000000..8099373 --- /dev/null +++ b/lab-python-flow-control.ipynb @@ -0,0 +1,87 @@ +{ + "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": 14, + "id": "c2b91065", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "for product in products:\n", + " quant = int(input(f\"Enter the quantity of {product}: \"))\n", + " inventory[product] = quant\n", + "customer_orders = set()\n", + "while True:\n", + " order = input(\"Enter the name of a product to order: \")\n", + " customer_orders.add(order)\n", + " more = input(\"Do you want to order another product? (yes/no): \")\n", + " if more.lower() != \"yes\":\n", + " break\n", + "for order in customer_orders:\n", + " if order in inventory.keys():\n", + " inventory[order] = inventory[order] - 1" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}