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..5b37119 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -0,0 +1,195 @@ +{ + "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": 32, + "id": "6c520ae1-4df5-43ee-ab20-526ac9c48a41", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the quantity of t-shirt: 2\n", + "Please enter the quantity of mug: 2\n", + "Please enter the quantity of hat: 2\n", + "Please enter the quantity of book: 2\n", + "Please enter the quantity of keychain: 2\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "\n", + "for product in products:\n", + " quantity_of_product = int(input(f\"Please enter the quantity of {product}:\"))\n", + " inventory[product] = quantity_of_product\n", + "\n", + "customer_orders = set()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "92be4c23-ca28-4dfc-8f02-30d54f85e950", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of the product to order: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hat added to your order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product? (yes/no): no\n" + ] + } + ], + "source": [ + "while True:\n", + " order = input(\"Enter the name of the product to order: \").strip()\n", + "\n", + " if order not in products:\n", + " print(f\"{order} is not available. Please choose a valid product.\")\n", + " continue\n", + " \n", + " if order in customer_orders:\n", + " print(f\"{order} ir already in the order.\")\n", + " else:\n", + " customer_orders.add(order)\n", + " print(f\"{order} added to your order.\")\n", + "\n", + " another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " if another != \"yes\":\n", + " break\n" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "67238a4f-efbc-4030-9550-2e245a645400", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'hat'}" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "1bfcb870-b63d-495f-b521-d6c5af0d5787", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer Order: {'hat'}\n", + "Updated Inventory:\n", + "t-shirt: 2\n", + "mug: 2\n", + "hat: 1\n", + "book: 2\n", + "keychain: 2\n" + ] + } + ], + "source": [ + "print(f\"Customer Order:\", customer_orders)\n", + "print(f\"Updated Inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7c78ff54-07ef-47ee-9e62-e3a7d7087594", + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..5b37119 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,6 +37,138 @@ "\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": 32, + "id": "6c520ae1-4df5-43ee-ab20-526ac9c48a41", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the quantity of t-shirt: 2\n", + "Please enter the quantity of mug: 2\n", + "Please enter the quantity of hat: 2\n", + "Please enter the quantity of book: 2\n", + "Please enter the quantity of keychain: 2\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "\n", + "for product in products:\n", + " quantity_of_product = int(input(f\"Please enter the quantity of {product}:\"))\n", + " inventory[product] = quantity_of_product\n", + "\n", + "customer_orders = set()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "92be4c23-ca28-4dfc-8f02-30d54f85e950", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of the product to order: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hat added to your order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product? (yes/no): no\n" + ] + } + ], + "source": [ + "while True:\n", + " order = input(\"Enter the name of the product to order: \").strip()\n", + "\n", + " if order not in products:\n", + " print(f\"{order} is not available. Please choose a valid product.\")\n", + " continue\n", + " \n", + " if order in customer_orders:\n", + " print(f\"{order} ir already in the order.\")\n", + " else:\n", + " customer_orders.add(order)\n", + " print(f\"{order} added to your order.\")\n", + "\n", + " another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " if another != \"yes\":\n", + " break\n" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "67238a4f-efbc-4030-9550-2e245a645400", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'hat'}" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "1bfcb870-b63d-495f-b521-d6c5af0d5787", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer Order: {'hat'}\n", + "Updated Inventory:\n", + "t-shirt: 2\n", + "mug: 2\n", + "hat: 1\n", + "book: 2\n", + "keychain: 2\n" + ] + } + ], + "source": [ + "print(f\"Customer Order:\", customer_orders)\n", + "print(f\"Updated Inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7c78ff54-07ef-47ee-9e62-e3a7d7087594", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -55,7 +187,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4, diff --git a/updated-lab-python-flow-control.ipynb b/updated-lab-python-flow-control.ipynb new file mode 100644 index 0000000..e38efc9 --- /dev/null +++ b/updated-lab-python-flow-control.ipynb @@ -0,0 +1,182 @@ +{ + "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": "6c520ae1-4df5-43ee-ab20-526ac9c48a41", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the quantity of t-shirt: 2\n", + "Please enter the quantity of mug: 2\n", + "Please enter the quantity of hat: 2\n", + "Please enter the quantity of book: 2\n", + "Please enter the quantity of keychain: 2\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "\n", + "for product in products:\n", + " quantity_of_product = int(input(f\"Please enter the quantity of {product}:\"))\n", + " inventory[product] = quantity_of_product\n", + "\n", + "customer_orders = set()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "92be4c23-ca28-4dfc-8f02-30d54f85e950", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of the product to order: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hat added to your order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product? (yes/no): no\n" + ] + } + ], + "source": [ + "while True:\n", + " order = input(\"Enter the name of the product to order: \").strip()\n", + "\n", + " if order not in products:\n", + " print(f\"{order} is not available. Please choose a valid product.\")\n", + " continue\n", + " \n", + " if order in customer_orders:\n", + " print(f\"{order} ir already in the order.\")\n", + " else:\n", + " customer_orders.add(order)\n", + " print(f\"{order} added to your order.\")\n", + "\n", + " another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " if another != \"yes\":\n", + " break\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "67238a4f-efbc-4030-9550-2e245a645400", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer Order: {'hat'}\n", + "Updated Inventory:\n", + "t-shirt: 2\n", + "mug: 2\n", + "hat: 1\n", + "book: 2\n", + "keychain: 2\n" + ] + } + ], + "source": [ + "customer_orders\n", + "\n", + "for product in customer_orders:\n", + " if inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " else:\n", + " print(f\"Sorry, {product} is out of stock!\")\n", + "\n", + "print(f\"Customer Order:\", customer_orders)\n", + "print(f\"Updated Inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7c78ff54-07ef-47ee-9e62-e3a7d7087594", + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}