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..b5bbaba --- /dev/null +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -0,0 +1,199 @@ +{ + "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": 1, + "id": "9e225ef9-d0b2-42a8-8438-7daafdbe8fab", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "3015afd7-593e-452b-99d4-ace2a4def125", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "6cf14d19-5645-4cad-8e63-7ea9c071bc8b", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Quantity of mug: 50\n", + "Quantity of hat: 20\n", + "Quantity of book: 24\n", + "Quantity of keychain: 10\n" + ] + } + ], + "source": [ + "inventory[\"mug\"] = int(input(\"Quantity of mug: \"))\n", + "inventory[\"hat\"] = int(input(\"Quantity of hat: \"))\n", + "inventory[\"book\"] = int(input(\"Quantity of book: \"))\n", + "inventory[\"keychain\"] = int(input(\"Quantity of keychain: \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "76651fc5-058c-4212-b079-280e882d5200", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Quantity of mug: 50\n", + "Quantity of hat: 20\n", + "Quantity of book: 24\n", + "Quantity of keychain: 10\n" + ] + } + ], + "source": [ + "for product in products:\n", + " inventory[product] = int(input(f\"Quantity of {product}: \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "fed47e1b-c06d-42cb-bca1-9c22fa214739", + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "f8f9ae44-7fd2-4652-9077-422fb05111fc", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product: mug\n", + "Do you want another product? (Yes/No) yes\n", + "Enter product: book\n", + "Do you want another product? (yes/no): no\n" + ] + } + ], + "source": [ + "product = input(\"Enter product: \") \n", + "customer_orders.add(product)\n", + "answer = input(\"Do you want another product? (Yes/No) \") \n", + "while answer == \"yes\":\n", + " product = input(\"Enter product: \")\n", + " customer_orders.add(product)\n", + " answer = input(\"Do you want another product? (yes/no): \")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "f4eacc07-c120-4738-941e-25cdbd2c789b", + "metadata": {}, + "outputs": [], + "source": [ + "for product in inventory.keys():\n", + " if product in customer_orders:\n", + " inventory[product] -= 1 " + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "048b5d3e-c46a-4503-8079-ad82cbd2a1ee", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'mug': 49, 'hat': 20, 'book': 23, 'keychain': 10}" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] + } + ], + "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.14.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..b5bbaba 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,6 +37,142 @@ "\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": 1, + "id": "9e225ef9-d0b2-42a8-8438-7daafdbe8fab", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "3015afd7-593e-452b-99d4-ace2a4def125", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "6cf14d19-5645-4cad-8e63-7ea9c071bc8b", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Quantity of mug: 50\n", + "Quantity of hat: 20\n", + "Quantity of book: 24\n", + "Quantity of keychain: 10\n" + ] + } + ], + "source": [ + "inventory[\"mug\"] = int(input(\"Quantity of mug: \"))\n", + "inventory[\"hat\"] = int(input(\"Quantity of hat: \"))\n", + "inventory[\"book\"] = int(input(\"Quantity of book: \"))\n", + "inventory[\"keychain\"] = int(input(\"Quantity of keychain: \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "76651fc5-058c-4212-b079-280e882d5200", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Quantity of mug: 50\n", + "Quantity of hat: 20\n", + "Quantity of book: 24\n", + "Quantity of keychain: 10\n" + ] + } + ], + "source": [ + "for product in products:\n", + " inventory[product] = int(input(f\"Quantity of {product}: \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "fed47e1b-c06d-42cb-bca1-9c22fa214739", + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "f8f9ae44-7fd2-4652-9077-422fb05111fc", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product: mug\n", + "Do you want another product? (Yes/No) yes\n", + "Enter product: book\n", + "Do you want another product? (yes/no): no\n" + ] + } + ], + "source": [ + "product = input(\"Enter product: \") \n", + "customer_orders.add(product)\n", + "answer = input(\"Do you want another product? (Yes/No) \") \n", + "while answer == \"yes\":\n", + " product = input(\"Enter product: \")\n", + " customer_orders.add(product)\n", + " answer = input(\"Do you want another product? (yes/no): \")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "f4eacc07-c120-4738-941e-25cdbd2c789b", + "metadata": {}, + "outputs": [], + "source": [ + "for product in inventory.keys():\n", + " if product in customer_orders:\n", + " inventory[product] -= 1 " + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "048b5d3e-c46a-4503-8079-ad82cbd2a1ee", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'mug': 49, 'hat': 20, 'book': 23, 'keychain': 10}" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] } ], "metadata": { @@ -55,7 +191,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.5" } }, "nbformat": 4,