diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..01a98db 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,11 +37,161 @@ "\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": "f909e40c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics: Total Products Ordered: 3, Percentage of Products Ordered: 60 %\n", + "Updated Inventory:\n", + "t-shirts: 3\n", + "mugs: 3\n", + "hats: 4\n", + "books: 5\n", + "keychains: 7\n" + ] + } + ], + "source": [ + "#1. Look at your code from the lab data structures, and improve repeated code with loops.\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "customer_orders = {}\n", + "\n", + "for product in products:\n", + " inventory[product] = int(input(\"Enter the number of \" + product + \"s: \"))\n", + "\n", + "for i in range(3):\n", + " order = input(\"What's product \" + str(i + 1) + \" you'd like to order: \").strip()\n", + " if order in products:\n", + " if order in customer_orders:\n", + " customer_orders[order] += 1\n", + " else:\n", + " customer_orders[order] = 1\n", + " else:\n", + " print(\"Invalid product: \" + order + \". Please enter a valid product.\")\n", + "\n", + "number_of_orders = 0\n", + "for order in customer_orders:\n", + " number_of_orders += customer_orders[order]\n", + "\n", + "percentage_of_orders = (number_of_orders / len(products)) * 100\n", + "print(\"Order Statistics: Total Products Ordered: \" + str(number_of_orders) + \", Percentage of Products Ordered: \" + str(int(percentage_of_orders)) + \" %\")\n", + "\n", + "for product in products:\n", + " if product in customer_orders:\n", + " inventory[product] -= customer_orders[product]\n", + "\n", + "print(\"Updated Inventory:\")\n", + "for product in products:\n", + " print(product + \"s: \" + str(inventory[product]))" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "360f0344", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer Orders: {'mug'}\n" + ] + } + ], + "source": [ + "\"\"\"2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:\n", + " a. Prompt the user to enter the name of a product that a customer wants to order.\n", + " b. Add the product name to the \"customer_orders\" set.\n", + " c. Ask the user if they want to add another product (yes/no).\n", + " d. Continue the loop until the user does not want to add another product.\n", + "\n", + "\"\"\"\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "\n", + "for product in products:\n", + " inventory[product] = int(input(\"Enter the number of \" + product + \"s: \"))\n", + "\n", + "customer_orders = set()\n", + "\n", + "while True:\n", + " order = input(\"Enter the name of a product that a customer wants to order: \").strip()\n", + " \n", + " if order in products:\n", + " customer_orders.add(order) \n", + " \n", + " another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " if another != 'yes':\n", + " break\n", + "\n", + "print(\"Customer Orders:\", customer_orders)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dfb2e5f8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory:\n", + "t-shirts: 3\n", + "mugs: 3\n", + "hats: 5\n", + "books: 5\n", + "keychains: 7\n" + ] + } + ], + "source": [ + "#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\").\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "\n", + "for product in products:\n", + " inventory[product] = int(input(\"Enter the number of \" + product + \"s: \"))\n", + "\n", + "customer_orders = set()\n", + "\n", + "while True:\n", + " order = input(\"Enter the name of a product that a customer wants to order: \").strip()\n", + " \n", + " if order in products:\n", + " customer_orders.add(order) \n", + " \n", + " another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " if another != 'yes':\n", + " break\n", + "\n", + "# Update inventory only for ordered products\n", + "for product in customer_orders:\n", + " if inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " else:\n", + " print(\"No more \" + product + \"s left in inventory.\")\n", + "\n", + "print(\"Updated Inventory:\")\n", + "for product in products:\n", + " print(product + \"s: \" + str(inventory[product]))" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -55,7 +205,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,