diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..5a8bbfb 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -1,63 +1,160 @@ { - "cells": [ - { - "cell_type": "markdown", - "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f", - "metadata": { - "tags": [] - }, - "source": [ - "# Lab | Flow Control" - ] + "cells": [ + { + "cell_type": "markdown", + "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f", + "metadata": { + "tags": [], + "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f" + }, + "source": [ + "# Lab | Flow Control" + ] + }, + { + "cell_type": "markdown", + "id": "3851fcd1-cf98-4653-9c89-e003b7ec9400", + "metadata": { + "id": "3851fcd1-cf98-4653-9c89-e003b7ec9400" + }, + "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", + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = {}\n", + "\n", + "for product in products:\n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + "\n", + "\n", + "customer_orders = set()\n", + "\n", + "add_more = \"yes\"\n", + "\n", + "while add_more.lower() == \"yes\":\n", + " order = input(\"Enter a product to order: \").strip()\n", + "\n", + " if order in products:\n", + " customer_orders.add(order)\n", + " else:\n", + " print(f\"{order} is not a valid product.\")\n", + "\n", + " add_more = input(\"Do you want to add another product? (yes/no): \").strip()\n", + "\n", + "\n", + "print(customer_orders)\n", + "\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "\n", + "\n", + "print(\"Order Statistics:\")\n", + "print(\"Total Products Ordered:\", order_status[0])\n", + "print(f\"Percentage of Products Ordered: {order_status[1]:.0f}%\")\n", + "\n", + "\n", + "for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + "\n", + "\n", + "print(\"Updated inventory:\")\n", + "\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "dpo2NgGuj4n7", + "outputId": "3c59e586-d9a3-45cb-820c-cce610a37bb5" + }, + "id": "dpo2NgGuj4n7", + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter quantity for t-shirt: 4\n", + "Enter quantity for mug: 5\n", + "Enter quantity for hat: 6\n", + "Enter quantity for book: 7\n", + "Enter quantity for keychain: 8\n", + "Enter a product to order: mug\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product to order: book\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product to order: hat\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product to order: keychain\n", + "Do you want to add another product? (yes/no): no\n", + "{'book', 'mug', 'hat', 'keychain'}\n", + "Order Statistics:\n", + "Total Products Ordered: 4\n", + "Percentage of Products Ordered: 80%\n", + "Updated inventory:\n", + "t-shirt: 4\n", + "mug: 4\n", + "hat: 5\n", + "book: 6\n", + "keychain: 7\n" + ] + } + ] + } + ], + "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.9.13" + }, + "colab": { + "provenance": [] + } }, - { - "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\")." - ] - } - ], - "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.9.13" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} + "nbformat": 4, + "nbformat_minor": 5 +} \ No newline at end of file