From d10144ecb1824544f48917f8140cd64b66883c72 Mon Sep 17 00:00:00 2001 From: Mandana Soltani Date: Sun, 8 Mar 2026 17:31:06 +0100 Subject: [PATCH] Add files via upload --- lab_python_flow_control.ipynb | 395 ++++++++++++++++++++++++++++++++++ 1 file changed, 395 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..281571b --- /dev/null +++ b/lab_python_flow_control.ipynb @@ -0,0 +1,395 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f", + "metadata": { + "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", + "execution_count": null, + "id": "sgFC2DgLQ0zy", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "sgFC2DgLQ0zy", + "outputId": "414f791a-5453-4e70-9d9c-ed36b3e10ff8" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the quantity for each product:\n", + "Enter the quantity of t-shirt : 8\n", + "Enter the quantity of mug, : 5\n", + "Enter the quantity of hat: 6\n", + "Enter the quantity of book: 3\n", + "Enter the quantity of keychain: 10\n" + ] + } + ], + "source": [ + "products = [\"t-shirt \", \"mug, \", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "customers_orders = set()\n", + "print(\"Enter the quantity for each product:\")\n", + "for product in products:\n", + " quantity = int(input(f\"Enter the quantity of {product}: \"))\n", + " inventory[product] = quantity\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3aQznE_8UDVo", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "3aQznE_8UDVo", + "outputId": "d6d322c3-6265-42dd-fe7a-86b6923a2ad7" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter a product you need to order:8\n", + "Invalid product. Please try again.\n", + "Do you want to add another product? (yes/no): no\n", + "{'8'}\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "customer_orders = set()\n", + "\n", + "while True:\n", + " product = input(\"Enter a product you need to order:\")\n", + "\n", + " if product not in products:\n", + " print(\"Invalid product. Please try again.\")\n", + "\n", + " customer_orders.add(product)\n", + "\n", + " another = input(\"Do you want to add another product? (yes/no): \")\n", + " if another == \"no\":\n", + " break\n", + "\n", + "print(customer_orders)\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "de01ca15-f0da-47d3-93f4-0f9090d7becd", + "id": "ewdIGVdHM8Uj" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the quantity for each product:\n", + "Enter the quantity of t-shirt : 3\n", + "Enter the quantity of mug, : 2\n", + "Enter the quantity of hat: 5\n", + "Enter the quantity of book: 20\n", + "Enter the quantity of keychain: 10\n" + ] + } + ], + "source": [ + "products = [\"t-shirt \", \"mug, \", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "customers_orders = set()\n", + "print(\"Enter the quantity for each product:\")\n", + "for product in products:\n", + " quantity = int(input(f\"Enter the quantity of {product}: \"))\n", + " inventory[product] = quantity\n", + "\n" + ], + "id": "ewdIGVdHM8Uj" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "37ffa49c-ba50-4293-ad0f-45b7bf17e549", + "id": "SneSDKaFMz90" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the quantity for each product:\n", + "Enter the quantity of t-shirt : 5\n", + "Enter the quantity of mug, : 5\n", + "Enter the quantity of hat: 1\n", + "Enter the quantity of book: 2\n", + "Enter the quantity of keychain: 3\n" + ] + } + ], + "source": [ + "products = [\"t-shirt \", \"mug, \", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "customers_orders = set()\n", + "print(\"Enter the quantity for each product:\")\n", + "for product in products:\n", + " quantity = int(input(f\"Enter the quantity of {product}: \"))\n", + " inventory[product] = quantity\n", + "\n" + ], + "id": "SneSDKaFMz90" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "0cd2f36c-f488-485a-9980-109b88cfc39f", + "id": "IATU3hnTMwO-" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter the quantity for each product:\n", + "Enter the quantity of t-shirt: 5\n", + "Enter the quantity of mug: 3\n", + "Enter the quantity of hat: 5\n", + "Enter the quantity of book: 2\n", + "Enter the quantity of keychain: 1\n", + "Inventory: {'t-shirt': 5, 'mug': 3, 'hat': 5, 'book': 2, 'keychain': 1}\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "customers_orders = set()\n", + "\n", + "print(\"Enter the quantity for each product:\")\n", + "\n", + "for product in products:\n", + " quantity = int(input(f\"Enter the quantity of {product}: \"))\n", + " inventory[product] = quantity\n", + "\n", + "print(\"Inventory:\",inventory )\n" + ], + "id": "IATU3hnTMwO-" + }, + { + "cell_type": "code", + "execution_count": null, + "id": "gr5K4EvLWbJC", + "metadata": { + "id": "gr5K4EvLWbJC", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "8d996316-ef6b-48e0-c823-16cb4f7e2ddd" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "\n", + "Customer ordered: {'8'}\n", + "order Status: (1, 20.0)\n" + ] + } + ], + "source": [ + "print(\"\\nCustomer ordered:\", customer_orders)\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "print(\"order Status:\", order_status)\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "pYtCol0rZQpc", + "metadata": { + "id": "pYtCol0rZQpc", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "0fe4e975-f7a4-4428-f4c9-dc0726019e11" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "order statistics:\n", + "Total products ordered: 1\n", + "Percentage of products ordered: 20.00%\n" + ] + } + ], + "source": [ + "print(\"order statistics:\")\n", + "print(f\"Total products ordered: {order_status[0]}\")\n", + "print(f\"Percentage of products ordered: {order_status[1]:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "MgpYsqe2alLA", + "metadata": { + "id": "MgpYsqe2alLA", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "73b13025-f13b-470a-a0b8-b16ce92285c8" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Warning: Product '8' in customer orders but not in inventory. Skipping update.\n" + ] + } + ], + "source": [ + "for Product in customer_orders:\n", + " if Product in inventory:\n", + " inventory[Product] -= 1\n", + " else:\n", + " print(f\"Warning: Product '{Product}' in customer orders but not in inventory. Skipping update.\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "S-6kIJUybuLr", + "metadata": { + "id": "S-6kIJUybuLr", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "5135255b-ab92-40bb-8406-317e885ccafa" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "updated inventory:\n", + "t-shirt: 5\n", + "mug: 3\n", + "hat: 5\n", + "book: 2\n", + "keychain: 1\n" + ] + } + ], + "source": [ + "print(\"updated inventory:\")\n", + "\n", + "for product, quantity in inventory.items():\n", + " print(f'{product}: {quantity}')\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "_zQr7IE5cFi1", + "metadata": { + "id": "_zQr7IE5cFi1" + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "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 +} \ No newline at end of file