From 0cbdf9f6c094309811d41727fdecf868410a6fec Mon Sep 17 00:00:00 2001 From: Pariya Date: Tue, 15 Sep 2026 17:07:56 +0300 Subject: [PATCH] solved the Lab --- lab-python-flow-control.ipynb | 221 +++++++++++++++++++++++++++++++++- 1 file changed, 219 insertions(+), 2 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..f793e89 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,11 +37,228 @@ "\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": "markdown", + "id": "197dd9c7", + "metadata": {}, + "source": [ + "improve repeated code with loops" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "b7209dae", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}" + ] + }, + { + "cell_type": "markdown", + "id": "fbd2d355", + "metadata": {}, + "source": [ + "Ask the user to input the quantity of each product available in the inventory. Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "9f4a8ccc", + "metadata": {}, + "outputs": [], + "source": [ + "for product in products:\n", + " inventory[product]=int(input(f\"Enter qualntity for {product}:\"))" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "687fe74f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 5, 'mug': 6, 'hat': 8, 'book': 7, 'keychain': 5}" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] + }, + { + "cell_type": "markdown", + "id": "d2569e00", + "metadata": {}, + "source": [ + "Prompt the user to enter the name of a product that a customer wants to order." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "b117d692", + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders=set()\n", + "for orders in range(3):\n", + " customer_orders.add(input(\"please inter a product name:\"))" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "89812a0e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'book', 'hat', 'mug'}" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders" + ] + }, + { + "cell_type": "markdown", + "id": "7d857416", + "metadata": {}, + "source": [ + "Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n", + "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": 7, + "id": "4e5f61b4", + "metadata": {}, + "outputs": [], + "source": [ + "for item in customer_orders:\n", + " inventory[item]=inventory[item]-1" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "221a746d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 5, 'mug': 5, 'hat': 7, 'book': 6, 'keychain': 5}" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] + }, + { + "cell_type": "markdown", + "id": "75d3e89f", + "metadata": {}, + "source": [ + "Print the updated inventory, displaying the quantity of each product on separate lines." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "b80e2891", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " t-shirt : 5\n", + " mug : 5\n", + " hat : 7\n", + " book : 6\n", + " keychain : 5\n" + ] + } + ], + "source": [ + "for product in inventory:\n", + " print(f\" {product} : {inventory[product]}\")" + ] + }, + { + "cell_type": "markdown", + "id": "29020f38", + "metadata": {}, + "source": [ + "Ask the user if they want to add another product (yes/no).\n", + "Continue the loop until the user does not want to add another product." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "6a97cbec", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "answer= \"yes\" \n", + "while answer == \"yes\":\n", + " product_name=input(\"please enter a product name:\")\n", + " customer_orders.add(product_name)\n", + " answer = input(\"do you want to add another product? (yes/no): \")" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "c3967f65", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'book', 'hat', 'mug', 't-shirt'}" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -55,7 +272,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,