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..84912dc --- /dev/null +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -0,0 +1,224 @@ +{ + "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": "f6681869-e087-4f6e-b273-6982dd572a11", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "3c380829-8b40-4176-bfb0-b3cc5f1f402d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{}\n" + ] + } + ], + "source": [ + "inventory = {}\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "58a20bdf-040f-4f24-abea-ee28e6445200", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the available quantity for each product:\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "t-shirt: 4\n", + "mug: 6\n", + "hat: 4\n", + "book: 6\n", + "keychain: 9\n" + ] + } + ], + "source": [ + "print(\"Enter the available quantity for each product:\")\n", + "for product in products:\n", + " quantity = int(input(f\"{product}: \"))\n", + " inventory[product] = quantity" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "042a519b-a49d-44c0-81b5-5aa10e02da59", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set()\n" + ] + } + ], + "source": [ + "customer_orders = set()\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "e9eba9ea-4f48-45e1-9a8f-3a376802d597", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product to order: hat\n", + "Do you want to add another product? (yes/no): book\n" + ] + } + ], + "source": [ + "while True:\n", + " product_name = input(\"Enter the name of a product to order: \").lower()\n", + "\n", + " if product_name in products:\n", + " customer_orders.add(product_name)\n", + " else:\n", + " print(\"product not found in store.\")\n", + "\n", + " another = input(\"Do you want to add another product? (yes/no): \").lower()\n", + " if another != \"yes\":\n", + " break" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "9e5efe7d-bf66-4579-8b73-de450ccf9246", + "metadata": {}, + "outputs": [], + "source": [ + "for product in customer_orders:\n", + " if inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " else:\n", + " print(f\"{product} is out of stock!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "a30f2e58-c16d-46cc-8f60-a3dc4bdad2df", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Customer Orders: {'hat'}\n", + "Updated Inventory: {'t-shirt': 4, 'mug': 6, 'hat': 3, 'book': 6, 'keychain': 9}\n" + ] + } + ], + "source": [ + "print(\"\\nCustomer Orders:\", customer_orders)\n", + "print(\"Updated Inventory:\", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d466fa23-f570-49b0-bbbb-07ac724b4f2f", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..84912dc 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,13 +37,174 @@ "\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": "f6681869-e087-4f6e-b273-6982dd572a11", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "3c380829-8b40-4176-bfb0-b3cc5f1f402d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{}\n" + ] + } + ], + "source": [ + "inventory = {}\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "58a20bdf-040f-4f24-abea-ee28e6445200", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the available quantity for each product:\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "t-shirt: 4\n", + "mug: 6\n", + "hat: 4\n", + "book: 6\n", + "keychain: 9\n" + ] + } + ], + "source": [ + "print(\"Enter the available quantity for each product:\")\n", + "for product in products:\n", + " quantity = int(input(f\"{product}: \"))\n", + " inventory[product] = quantity" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "042a519b-a49d-44c0-81b5-5aa10e02da59", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set()\n" + ] + } + ], + "source": [ + "customer_orders = set()\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "e9eba9ea-4f48-45e1-9a8f-3a376802d597", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product to order: hat\n", + "Do you want to add another product? (yes/no): book\n" + ] + } + ], + "source": [ + "while True:\n", + " product_name = input(\"Enter the name of a product to order: \").lower()\n", + "\n", + " if product_name in products:\n", + " customer_orders.add(product_name)\n", + " else:\n", + " print(\"product not found in store.\")\n", + "\n", + " another = input(\"Do you want to add another product? (yes/no): \").lower()\n", + " if another != \"yes\":\n", + " break" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "9e5efe7d-bf66-4579-8b73-de450ccf9246", + "metadata": {}, + "outputs": [], + "source": [ + "for product in customer_orders:\n", + " if inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " else:\n", + " print(f\"{product} is out of stock!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "a30f2e58-c16d-46cc-8f60-a3dc4bdad2df", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Customer Orders: {'hat'}\n", + "Updated Inventory: {'t-shirt': 4, 'mug': 6, 'hat': 3, 'book': 6, 'keychain': 9}\n" + ] + } + ], + "source": [ + "print(\"\\nCustomer Orders:\", customer_orders)\n", + "print(\"Updated Inventory:\", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d466fa23-f570-49b0-bbbb-07ac724b4f2f", + "metadata": {}, + "outputs": [], + "source": [] } ], "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 +216,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,