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..f4c7391 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -0,0 +1,63 @@ +{ + "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\")." + ] + } + ], + "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 +} diff --git a/anaconda_projects/db/project_filebrowser.db b/anaconda_projects/db/project_filebrowser.db new file mode 100644 index 0000000..3fa3a4a Binary files /dev/null and b/anaconda_projects/db/project_filebrowser.db differ diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..418f663 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,13 +37,148 @@ "\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": "96a1ae08-0375-4799-9fae-c009b9da17ae", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "--- Welcome to the Optimized Order System ---\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "\n", + "Enter the name of a product to order (e.g., Laptop, Mouse): Laptop, Mouse, Monitor, Webcam\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'Laptop, Mouse, Monitor, Webcam' is not a recognized product in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product to the order? (yes/no): yes\n", + "\n", + "Enter the name of a product to order (e.g., Laptop, Mouse): Laptop\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'Laptop' added to the order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to add another product to the order? (yes/no): Mouse\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "--- Order Collection Complete ---\n", + "Products Ordered: {'Laptop'}\n", + "Current Inventory Before Update: {'Laptop': 10, 'Mouse': 25, 'Keyboard': 15, 'Monitor': 8, 'Webcam': 20}\n", + "\n", + "--- Updating Inventory ---\n", + "Inventory for 'Laptop' updated. New quantity: 9\n", + "\n", + "--- Final Inventory Status ---\n", + "{'Laptop': 9, 'Mouse': 25, 'Keyboard': 15, 'Monitor': 8, 'Webcam': 20}\n" + ] + } + ], + "source": [ + "# Initial Inventory (simulating the data structure from your previous lab)\n", + "inventory = {\n", + " \"Laptop\": 10,\n", + " \"Mouse\": 25,\n", + " \"Keyboard\": 15,\n", + " \"Monitor\": 8,\n", + " \"Webcam\": 20\n", + "}\n", + "\n", + "# Set to store the products ordered by the customer\n", + "customer_orders = set()\n", + "\n", + "print(\"--- Welcome to the Optimized Order System ---\")\n", + "\n", + "# Step 1: Use a while loop to collect an arbitrary number of orders\n", + "# a, b, c, d: Prompt, Add, Ask, Continue\n", + "while True:\n", + " # a. Prompt the user to enter the name of a product\n", + " product_name = input(\"\\nEnter the name of a product to order (e.g., Laptop, Mouse): \").strip()\n", + " \n", + " # Optional: Basic validation to check if the product is in inventory\n", + " if product_name in inventory:\n", + " # b. Add the product name to the \"customer_orders\" set\n", + " customer_orders.add(product_name)\n", + " print(f\"'{product_name}' added to the order.\")\n", + " else:\n", + " print(f\"'{product_name}' is not a recognized product in the inventory.\")\n", + "\n", + " # c. Ask the user if they want to add another product\n", + " add_more = input(\"Do you want to add another product to the order? (yes/no): \").strip().lower()\n", + "\n", + " # d. Continue the loop until the user does not want to add another product\n", + " if add_more != 'yes':\n", + " break\n", + "\n", + "print(\"\\n--- Order Collection Complete ---\")\n", + "print(\"Products Ordered:\", customer_orders)\n", + "print(\"Current Inventory Before Update:\", inventory)\n", + "\n", + "# Step 2: Use a for loop to update the inventory only for ordered products\n", + "# We assume an order is for 1 unit of each product type for simplicity,\n", + "# as per the original requirement to subtract 1 from the quantity.\n", + "print(\"\\n--- Updating Inventory ---\")\n", + "for product in customer_orders:\n", + " # Check if the product is in the inventory just in case (though it should be)\n", + " if product in inventory:\n", + " if inventory[product] > 0:\n", + " # Update the inventory by subtracting 1\n", + " inventory[product] -= 1\n", + " print(f\"Inventory for '{product}' updated. New quantity: {inventory[product]}\")\n", + " else:\n", + " print(f\"Warning: '{product}' is out of stock and could not be fulfilled.\")\n", + " \n", + "# Final output\n", + "print(\"\\n--- Final Inventory Status ---\")\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3cb0a9f8-241c-4286-b3fd-8ecc95b82119", + "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 +190,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,