diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..2116dc8 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -1,49 +1,180 @@ { "cells": [ { - "cell_type": "markdown", - "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f", - "metadata": { - "tags": [] - }, + "cell_type": "code", + "execution_count": 2, + "id": "2ad532e5-9072-4aa4-baca-7f37cb81f156", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for t-shirt: 40\n", + "Enter quantity for mug: 20\n", + "Enter quantity for hat: 10\n", + "Enter quantity for book: 18\n", + "Enter quantity for keychain: 88\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "for product in products:\n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " inventory[product] = quantity" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "fbd9515d-d9d4-4cdc-88e8-2087de44ac72", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product to order: mug\n", + "Do you want to add another_product? (yes/no: yes\n", + "Enter the name of a product to order: hut\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "invalid product. Choose from: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product to order: book\n", + "Do you want to add another_product? (yes/no: yes\n", + "Enter the name of a product to order: keychain\n", + "Do you want to add another_product? (yes/no: no\n" + ] + } + ], "source": [ - "# Lab | Flow Control" + "customer_orders = set()\n", + "while True:\n", + " order = input(\"Enter the name of a product to order: \").lower()\n", + " if order in products:\n", + " customer_orders.add(order)\n", + " else:\n", + " print(\"invalid product. Choose from:\", products)\n", + " continue\n", + " another_product = input(\"Do you want to add another_product? (yes/no: \").lower()\n", + " if another_product == \"no\":\n", + " break" ] }, { - "cell_type": "markdown", - "id": "3851fcd1-cf98-4653-9c89-e003b7ec9400", + "cell_type": "code", + "execution_count": 16, + "id": "b3e0cc4c-33b7-4ec0-ad2a-034c1e05fb4f", "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Customer Orders:\n", + "{'book', 'keychain', 'mug'}\n", + "{'book', 'keychain', 'mug'}\n", + "{'book', 'keychain', 'mug'}\n" + ] + } + ], "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\")." + "print(\"\\nCustomer Orders:\")\n", + "for product in customer_orders:\n", + " print(customer_orders)" ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "eeee4b34-0921-43b8-a25e-090b1b2cb5c9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0%\n" + ] + } + ], + "source": [ + "total_products_ordered = len(customer_orders)\n", + "percentage_product_ordered = (total_products_ordered / len(products)) * 100\n", + "order_status = (total_products_ordered, percentage_product_ordered)\n", + "print(\"\\nOrder Statistics:\")\n", + "print(f\"Total Products Ordered: {order_status[0]}\")\n", + "print(f\"Percentage of Products Ordered: {order_status[1]}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "f7d03499-3ed9-46e9-af66-285295e80f0d", + "metadata": {}, + "outputs": [], + "source": [ + "for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "8af31852-a3fa-4765-84aa-14466dabfac1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Inventory:\n", + "t-shirt: 40\n", + "mug: 17\n", + "hat: 10\n", + "book: 15\n", + "keychain: 85\n" + ] + } + ], + "source": [ + "print(\"\\nUpdated Inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "88ce8150-c4c7-4cbf-b60d-0077cd63689e", + "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 +186,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,