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..ab193ca --- /dev/null +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -0,0 +1,184 @@ +{ + "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": 25, + "id": "126d8f91-8b5a-4b72-967f-9c50c1d6af20", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = {\n", + " \"t-shirt\": 15,\n", + " \"mug\": 20,\n", + " \"hat\": 10,\n", + " \"book\": 4,\n", + " \"keychain\": 13}\n", + "\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "00ba90bc-1abd-4091-84ae-87d873c3a6d5", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product: book\n", + "Would you like to add another product? (yes/no) yes\n", + "Enter a product: hat\n", + "Would you like to add another product? (yes/no) yes\n", + "Enter a product: hola\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product not available\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Would you like to add another product? (yes/no) no\n" + ] + } + ], + "source": [ + "while True:\n", + " product = input(\"Enter a product: \")\n", + "\n", + " if product in products:\n", + " customer_orders.add(product)\n", + " else:\n", + " print(\"Product not available\")\n", + " \n", + " another = input(\"Would you like to add another product? (yes/no)\")\n", + "\n", + " if another == \"yes\":\n", + " if another in products:\n", + " customer_orders.add(product)\n", + " else:\n", + " break\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "e9399b33-5355-4fce-9d31-cdf830618407", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'book', 'hat'}" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "345e5f30-f878-4a7b-9478-a22b646ea153", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 15, 'mug': 20, 'hat': 9, 'book': 3, 'keychain': 13}\n" + ] + } + ], + "source": [ + "for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + "\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "762f85e0-96c0-4c5f-b51e-0cf4c3b346ce", + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..ab193ca 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,13 +37,134 @@ "\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": 25, + "id": "126d8f91-8b5a-4b72-967f-9c50c1d6af20", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = {\n", + " \"t-shirt\": 15,\n", + " \"mug\": 20,\n", + " \"hat\": 10,\n", + " \"book\": 4,\n", + " \"keychain\": 13}\n", + "\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "00ba90bc-1abd-4091-84ae-87d873c3a6d5", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product: book\n", + "Would you like to add another product? (yes/no) yes\n", + "Enter a product: hat\n", + "Would you like to add another product? (yes/no) yes\n", + "Enter a product: hola\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product not available\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Would you like to add another product? (yes/no) no\n" + ] + } + ], + "source": [ + "while True:\n", + " product = input(\"Enter a product: \")\n", + "\n", + " if product in products:\n", + " customer_orders.add(product)\n", + " else:\n", + " print(\"Product not available\")\n", + " \n", + " another = input(\"Would you like to add another product? (yes/no)\")\n", + "\n", + " if another == \"yes\":\n", + " if another in products:\n", + " customer_orders.add(product)\n", + " else:\n", + " break\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "e9399b33-5355-4fce-9d31-cdf830618407", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'book', 'hat'}" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "345e5f30-f878-4a7b-9478-a22b646ea153", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 15, 'mug': 20, 'hat': 9, 'book': 3, 'keychain': 13}\n" + ] + } + ], + "source": [ + "for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + "\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "762f85e0-96c0-4c5f-b51e-0cf4c3b346ce", + "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 +176,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,