From 1a3031a3385f70a4fdfe42f17164486dcb6ff606 Mon Sep 17 00:00:00 2001 From: Vildan Pirpiroglu Date: Sun, 21 Jun 2026 12:37:16 +0300 Subject: [PATCH] Solved lab --- .../lab-python-flow-control-checkpoint.ipynb | 140 ++++++++++++++++++ lab-python-flow-control.ipynb | 79 +++++++++- 2 files changed, 218 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..fa51480 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -0,0 +1,140 @@ +{ + "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": 2, + "id": "d607ac42-252f-4974-a51a-5d02abf78d52", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 5\n", + "Enter the quantity of mugs available: 6\n", + "Enter the quantity of hats available: 7\n", + "Enter the quantity of books available: 3\n", + "Enter the quantity of keychains available: 4\n", + "Enter the name of a product the customer wants to order: hat\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter the name of a product the customer wants to order: mug\n", + "Do you want to add another product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Products in customer order: {'mug', 'hat'}\n", + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Products Ordered: 40.0%\n", + "Updated inventory:\n", + "t-shirt: 5\n", + "mug: 5\n", + "hat: 6\n", + "book: 3\n", + "keychain: 4\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "for product in products:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " inventory[product] = quantity\n", + "\n", + "customer_orders = set()\n", + "another = \"yes\"\n", + "while another == \"yes\":\n", + " order = input(\"Enter the name of a product the customer wants to order: \")\n", + " customer_orders.add(order)\n", + " another = input(\"Do you want to add another product? (yes/no): \")\n", + "\n", + "print(\"Products in customer order:\", customer_orders)\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", + "print(\"Order Statistics:\")\n", + "print(f\"Total Products Ordered: {order_status[0]}\")\n", + "print(f\"Percentage of Products Ordered: {order_status[1]}%\")\n", + "\n", + "for product in customer_orders:\n", + " inventory[product] -= 1\n", + "\n", + "print(\"Updated inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f64aacd2-3647-4004-9020-4936ee3866ee", + "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.12.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..fa51480 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,6 +37,83 @@ "\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": 2, + "id": "d607ac42-252f-4974-a51a-5d02abf78d52", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 5\n", + "Enter the quantity of mugs available: 6\n", + "Enter the quantity of hats available: 7\n", + "Enter the quantity of books available: 3\n", + "Enter the quantity of keychains available: 4\n", + "Enter the name of a product the customer wants to order: hat\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter the name of a product the customer wants to order: mug\n", + "Do you want to add another product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Products in customer order: {'mug', 'hat'}\n", + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Products Ordered: 40.0%\n", + "Updated inventory:\n", + "t-shirt: 5\n", + "mug: 5\n", + "hat: 6\n", + "book: 3\n", + "keychain: 4\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "for product in products:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " inventory[product] = quantity\n", + "\n", + "customer_orders = set()\n", + "another = \"yes\"\n", + "while another == \"yes\":\n", + " order = input(\"Enter the name of a product the customer wants to order: \")\n", + " customer_orders.add(order)\n", + " another = input(\"Do you want to add another product? (yes/no): \")\n", + "\n", + "print(\"Products in customer order:\", customer_orders)\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", + "print(\"Order Statistics:\")\n", + "print(f\"Total Products Ordered: {order_status[0]}\")\n", + "print(f\"Percentage of Products Ordered: {order_status[1]}%\")\n", + "\n", + "for product in customer_orders:\n", + " inventory[product] -= 1\n", + "\n", + "print(\"Updated inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f64aacd2-3647-4004-9020-4936ee3866ee", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -55,7 +132,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.9" } }, "nbformat": 4,