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..3faf3ed --- /dev/null +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -0,0 +1,174 @@ +{ + "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": 19, + "id": "814db1bf-8e04-4d3b-91d5-3fa9ad5203b5", + "metadata": {}, + "outputs": [], + "source": [ + "# previous disctionary\n", + "inventory = {'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "06020834-42e8-4603-81f0-f7c6c06fd651", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter a product name: mug\n", + "New order (Y/N): Y\n", + "Please enter a product name: hat\n", + "New order (Y/N): Y\n", + "Please enter a product name: book\n", + "New order (Y/N): N\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'book', 'mug'}\n" + ] + } + ], + "source": [ + "customer_product = input(\"Please enter a product name: \")\n", + "if customer_product in products:\n", + " customer_orders.add(customer_product)\n", + " new_order = input(\"New order (Y/N): \")\n", + " while new_order == \"Y\":\n", + " customer_product = input(\"Please enter a product name: \")\n", + " customer_orders.add(customer_product)\n", + " new_order = input(\"New order (Y/N): \")\n", + "else:\n", + " customer_product = input(\"Please enter 1 product among: \" + str(products))\n", + "print(customer_orders)\n", + "\n", + "# use case other than Y uncovered (like Wrong answer)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "e2b3b5e0-5baf-4c66-bf08-8f55ef4d79c4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n", + "True\n", + "True\n", + "False\n" + ] + } + ], + "source": [ + "for key in inventory:\n", + " if key in customer_orders:\n", + " print(\"True\")\n", + " else:\n", + " print(\"False\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "73dc2f8b-af99-4bdf-bdb6-0812abaaebe1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'mug': 10, 'hat': 10, 'book': 10}" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_inventory = {key:value for key,value in inventory.items() if key in customer_orders }\n", + "new_inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7a3de2b1-08f8-4380-8f45-d9db33ebec9b", + "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..3faf3ed 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,6 +37,117 @@ "\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": 19, + "id": "814db1bf-8e04-4d3b-91d5-3fa9ad5203b5", + "metadata": {}, + "outputs": [], + "source": [ + "# previous disctionary\n", + "inventory = {'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "06020834-42e8-4603-81f0-f7c6c06fd651", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter a product name: mug\n", + "New order (Y/N): Y\n", + "Please enter a product name: hat\n", + "New order (Y/N): Y\n", + "Please enter a product name: book\n", + "New order (Y/N): N\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'book', 'mug'}\n" + ] + } + ], + "source": [ + "customer_product = input(\"Please enter a product name: \")\n", + "if customer_product in products:\n", + " customer_orders.add(customer_product)\n", + " new_order = input(\"New order (Y/N): \")\n", + " while new_order == \"Y\":\n", + " customer_product = input(\"Please enter a product name: \")\n", + " customer_orders.add(customer_product)\n", + " new_order = input(\"New order (Y/N): \")\n", + "else:\n", + " customer_product = input(\"Please enter 1 product among: \" + str(products))\n", + "print(customer_orders)\n", + "\n", + "# use case other than Y uncovered (like Wrong answer)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "e2b3b5e0-5baf-4c66-bf08-8f55ef4d79c4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n", + "True\n", + "True\n", + "False\n" + ] + } + ], + "source": [ + "for key in inventory:\n", + " if key in customer_orders:\n", + " print(\"True\")\n", + " else:\n", + " print(\"False\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "73dc2f8b-af99-4bdf-bdb6-0812abaaebe1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'mug': 10, 'hat': 10, 'book': 10}" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_inventory = {key:value for key,value in inventory.items() if key in customer_orders }\n", + "new_inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7a3de2b1-08f8-4380-8f45-d9db33ebec9b", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -55,7 +166,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,