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..f889748 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -0,0 +1,240 @@ +{ + "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": 122, + "id": "a18f3710-1785-4c3e-b348-cf385685c0dc", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity available for t-shirt: 6\n", + "Enter quantity available for mug: 8\n", + "Enter quantity available for hat: 4\n", + "Enter quantity available for book: 8\n", + "Enter quantity available for keychain: 9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 6, 'mug': 8, 'hat': 4, 'book': 8, 'keychain': 9}\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "for product in products:\n", + " quantity = int(input(f\"Enter quantity available for {product}: \"))\n", + " inventory[product] = quantity\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 124, + "id": "0eff8979-3d3a-4b10-9226-92d55c4f3462", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of product for the order: mug\n", + "Enter the name of product for the order: hat\n", + "Enter the name of product for the order: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'mug', 'book'}\n" + ] + } + ], + "source": [ + "customer_orders = set()\n", + "for i in range(3):\n", + " order = input(\"Enter the name of product for the order: \")\n", + " if order in products:\n", + " customer_orders.add(order)\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 128, + "id": "8562f097-15ae-4ce0-9d84-4be5b757ad1b", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of product for the order: mug\n", + "Do you want to add another product: yes\n", + "Enter the name of product for the order: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product added.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product: yes\n", + "Enter the name of product for the order: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product added.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product: no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order is complete\n", + "{'hat', 'mug', 'book'}\n" + ] + } + ], + "source": [ + "customer_orders = set()\n", + "\n", + "new_order = input(\"Enter the name of product for the order: \")\n", + "if new_order in products:\n", + " customer_orders.add(new_order)\n", + "another_product = input(\"Do you want to add another product: \")\n", + "while another_product == \"yes\":\n", + " new_order = input(\"Enter the name of product for the order: \")\n", + " if new_order in products:\n", + " customer_orders.add(new_order)\n", + " print(\"Product added.\")\n", + " another_product = input(\"Do you want to add another product: \")\n", + "else:\n", + " print(\"Order is complete\")\n", + " \n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 129, + "id": "bcf9bf9b-e1ec-4726-8be4-606104df22ec", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 6, 'mug': 8, 'hat': 4, 'book': 8, 'keychain': 9}\n" + ] + } + ], + "source": [ + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 130, + "id": "6362718a-c707-4a59-93d8-5f3d730685d4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 6, 'mug': 7, 'hat': 3, 'book': 7, 'keychain': 9}\n" + ] + } + ], + "source": [ + "for order in customer_orders:\n", + " if order in inventory:\n", + " inventory[order] -= 1\n", + "print(inventory)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "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 +} diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..f889748 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,13 +37,190 @@ "\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": 122, + "id": "a18f3710-1785-4c3e-b348-cf385685c0dc", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity available for t-shirt: 6\n", + "Enter quantity available for mug: 8\n", + "Enter quantity available for hat: 4\n", + "Enter quantity available for book: 8\n", + "Enter quantity available for keychain: 9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 6, 'mug': 8, 'hat': 4, 'book': 8, 'keychain': 9}\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "for product in products:\n", + " quantity = int(input(f\"Enter quantity available for {product}: \"))\n", + " inventory[product] = quantity\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 124, + "id": "0eff8979-3d3a-4b10-9226-92d55c4f3462", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of product for the order: mug\n", + "Enter the name of product for the order: hat\n", + "Enter the name of product for the order: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'mug', 'book'}\n" + ] + } + ], + "source": [ + "customer_orders = set()\n", + "for i in range(3):\n", + " order = input(\"Enter the name of product for the order: \")\n", + " if order in products:\n", + " customer_orders.add(order)\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 128, + "id": "8562f097-15ae-4ce0-9d84-4be5b757ad1b", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of product for the order: mug\n", + "Do you want to add another product: yes\n", + "Enter the name of product for the order: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product added.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product: yes\n", + "Enter the name of product for the order: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product added.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product: no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order is complete\n", + "{'hat', 'mug', 'book'}\n" + ] + } + ], + "source": [ + "customer_orders = set()\n", + "\n", + "new_order = input(\"Enter the name of product for the order: \")\n", + "if new_order in products:\n", + " customer_orders.add(new_order)\n", + "another_product = input(\"Do you want to add another product: \")\n", + "while another_product == \"yes\":\n", + " new_order = input(\"Enter the name of product for the order: \")\n", + " if new_order in products:\n", + " customer_orders.add(new_order)\n", + " print(\"Product added.\")\n", + " another_product = input(\"Do you want to add another product: \")\n", + "else:\n", + " print(\"Order is complete\")\n", + " \n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 129, + "id": "bcf9bf9b-e1ec-4726-8be4-606104df22ec", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 6, 'mug': 8, 'hat': 4, 'book': 8, 'keychain': 9}\n" + ] + } + ], + "source": [ + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 130, + "id": "6362718a-c707-4a59-93d8-5f3d730685d4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 6, 'mug': 7, 'hat': 3, 'book': 7, 'keychain': 9}\n" + ] + } + ], + "source": [ + "for order in customer_orders:\n", + " if order in inventory:\n", + " inventory[order] -= 1\n", + "print(inventory)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -55,7 +232,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,