From 0e8fe6daeeec37018ff330eab0af1ca2a6bf9c31 Mon Sep 17 00:00:00 2001 From: Seena <“sinamail.ay@gmail.com”> Date: Thu, 2 Oct 2025 16:04:56 +0200 Subject: [PATCH] lab solved --- .../lab-python-flow-control-checkpoint.ipynb | 155 ++++++++++++++++++ lab-python-flow-control.ipynb | 94 ++++++++++- 2 files changed, 248 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..80dfebe --- /dev/null +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -0,0 +1,155 @@ +{ + "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": 3, + "id": "e0ea200c-7b08-44ec-8611-79a40eb359ea", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity for t-shirt: 5\n", + "Enter the quantity for mug: 5\n", + "Enter the quantity for hat: 5\n", + "Enter the quantity for book: 5\n", + "Enter the quantity for keychain: 5\n", + "Do you want to add a new product to your order? (yes or no): 5\n" + ] + } + ], + "source": [ + "#1 \n", + "products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "\n", + "for product in products:\n", + " quantity = int(input(f\"Enter the quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + "\n", + "customer_orders = set()\n", + "\n", + "while answer := input(\"Do you want to add a new product to your order? (yes or no): \") == \"yes\":\n", + " order = input(f\"Enter the name of product {len(customer_orders) + 1} to order: \")\n", + " if order in products:\n", + " customer_orders.add(order)\n", + " else:\n", + " print(f\"{order} is not a valid product.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "bea009c2-a3c8-4928-a672-91fc6f7966e9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Products in the customer orders:\n", + "Order Statistics:\n", + "Total Products Ordered: 0\n", + "Percentage of Products Ordered: 0.00%\n", + "Updated Inventory:\n", + "t-shirt: 5\n", + "mug: 2\n", + "hat: 1\n", + "book: 5\n", + "keychain: 4\n" + ] + } + ], + "source": [ + "#2\n", + "print(\"Products in the customer orders:\")\n", + "for item in customer_orders:\n", + " print(f\"- {item}\")\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "\n", + "print(\"Order Statistics:\")\n", + "print(f\"Total Products Ordered: {order_status[0]}\")\n", + "print(f\"Percentage of Products Ordered: {order_status[1]:.2f}%\")\n", + "\n", + "for item in customer_orders:\n", + " if item in inventory:\n", + " inventory[item] -= 1\n", + "\n", + "print(\"Updated Inventory:\")\n", + "for item, quantity in inventory.items():\n", + " print(f\"{item}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "447d5f61-d69b-460b-8372-55d7ae58fa4e", + "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..80dfebe 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,6 +37,98 @@ "\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": 3, + "id": "e0ea200c-7b08-44ec-8611-79a40eb359ea", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity for t-shirt: 5\n", + "Enter the quantity for mug: 5\n", + "Enter the quantity for hat: 5\n", + "Enter the quantity for book: 5\n", + "Enter the quantity for keychain: 5\n", + "Do you want to add a new product to your order? (yes or no): 5\n" + ] + } + ], + "source": [ + "#1 \n", + "products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "\n", + "for product in products:\n", + " quantity = int(input(f\"Enter the quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + "\n", + "customer_orders = set()\n", + "\n", + "while answer := input(\"Do you want to add a new product to your order? (yes or no): \") == \"yes\":\n", + " order = input(f\"Enter the name of product {len(customer_orders) + 1} to order: \")\n", + " if order in products:\n", + " customer_orders.add(order)\n", + " else:\n", + " print(f\"{order} is not a valid product.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "bea009c2-a3c8-4928-a672-91fc6f7966e9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Products in the customer orders:\n", + "Order Statistics:\n", + "Total Products Ordered: 0\n", + "Percentage of Products Ordered: 0.00%\n", + "Updated Inventory:\n", + "t-shirt: 5\n", + "mug: 2\n", + "hat: 1\n", + "book: 5\n", + "keychain: 4\n" + ] + } + ], + "source": [ + "#2\n", + "print(\"Products in the customer orders:\")\n", + "for item in customer_orders:\n", + " print(f\"- {item}\")\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "\n", + "print(\"Order Statistics:\")\n", + "print(f\"Total Products Ordered: {order_status[0]}\")\n", + "print(f\"Percentage of Products Ordered: {order_status[1]:.2f}%\")\n", + "\n", + "for item in customer_orders:\n", + " if item in inventory:\n", + " inventory[item] -= 1\n", + "\n", + "print(\"Updated Inventory:\")\n", + "for item, quantity in inventory.items():\n", + " print(f\"{item}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "447d5f61-d69b-460b-8372-55d7ae58fa4e", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -55,7 +147,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,