diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..45e677a Binary files /dev/null and b/.DS_Store differ 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..d813f93 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -0,0 +1,155 @@ +{ + "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": 6, + "id": "d9de3826-e669-437e-8449-e8650b9e40a6", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity available for t-shirt: 5\n", + "Enter the quantity available for mug: 5\n", + "Enter the quantity available for hat: 5\n", + "Enter the quantity available for book: 5\n", + "Enter the quantity available for keychain: 5\n", + "Enter the other product that customer wants: hat\n", + "Would you like to add another product?(Answer only Yes or No) no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer orders: {'hat'}\n", + "1\n", + "2.0\n", + "Order Statistics:\n", + "Total Products Ordered: 1\n", + "Percentage of Products Ordered: 2.0 %\n", + "{'t-shirt': 5, 'mug': 5, 'hat': 4, 'book': 5, 'keychain': 5}\n" + ] + } + ], + "source": [ + "#LAST LAB CODE:\n", + "#ANSWER1: We already use loops \n", + "\n", + "\n", + "# Step 1: Define the list of products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "# Step 2: Create empty inventory dictionary\n", + "inventory = {}\n", + "\n", + "# Ask user for quantity of each product \n", + "for product in products:\n", + " quantity = int(input(f\"Enter the quantity available for {product}: \"))\n", + " inventory[product] = quantity\n", + "\n", + " # Create empty set for customer orders\n", + "customer_orders = set()\n", + "\n", + "# Ask user for three products ------> ANSWER 2:\n", + "\n", + "while True:\n", + " order=input(\"Enter the other product that customer wants: \")\n", + " customer_orders.add(order)\n", + " another_product=input(\"Would you like to add another product?(Answer only Yes or No) \")\n", + "\n", + " if another_product==\"No\" or another_product==\"no\":\n", + " break\n", + " \n", + " # Print the products ordered\n", + "print(\"Customer orders:\", customer_orders)\n", + "\n", + "# Calculate order statistics\n", + "total_products_ordered = len(customer_orders)\n", + "print(total_products_ordered)\n", + "percentage_ordered = (total_products_ordered / len(products)) * 10\n", + "print(percentage_ordered)\n", + "order_status=(total_products_ordered,percentage_ordered)\n", + "\n", + "# Print statistics\n", + "print(\"Order Statistics:\")\n", + "print(\"Total Products Ordered:\", total_products_ordered)\n", + "print(\"Percentage of Products Ordered:\", percentage_ordered, \"%\")\n", + "\n", + "# Update inventory ------> ANSWER 3:Already set in the previous lab\n", + "for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -=1 \n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2b7e6124-3281-43e5-a167-14576347cc19", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "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.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..d813f93 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,13 +37,105 @@ "\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": 6, + "id": "d9de3826-e669-437e-8449-e8650b9e40a6", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity available for t-shirt: 5\n", + "Enter the quantity available for mug: 5\n", + "Enter the quantity available for hat: 5\n", + "Enter the quantity available for book: 5\n", + "Enter the quantity available for keychain: 5\n", + "Enter the other product that customer wants: hat\n", + "Would you like to add another product?(Answer only Yes or No) no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer orders: {'hat'}\n", + "1\n", + "2.0\n", + "Order Statistics:\n", + "Total Products Ordered: 1\n", + "Percentage of Products Ordered: 2.0 %\n", + "{'t-shirt': 5, 'mug': 5, 'hat': 4, 'book': 5, 'keychain': 5}\n" + ] + } + ], + "source": [ + "#LAST LAB CODE:\n", + "#ANSWER1: We already use loops \n", + "\n", + "\n", + "# Step 1: Define the list of products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "# Step 2: Create empty inventory dictionary\n", + "inventory = {}\n", + "\n", + "# Ask user for quantity of each product \n", + "for product in products:\n", + " quantity = int(input(f\"Enter the quantity available for {product}: \"))\n", + " inventory[product] = quantity\n", + "\n", + " # Create empty set for customer orders\n", + "customer_orders = set()\n", + "\n", + "# Ask user for three products ------> ANSWER 2:\n", + "\n", + "while True:\n", + " order=input(\"Enter the other product that customer wants: \")\n", + " customer_orders.add(order)\n", + " another_product=input(\"Would you like to add another product?(Answer only Yes or No) \")\n", + "\n", + " if another_product==\"No\" or another_product==\"no\":\n", + " break\n", + " \n", + " # Print the products ordered\n", + "print(\"Customer orders:\", customer_orders)\n", + "\n", + "# Calculate order statistics\n", + "total_products_ordered = len(customer_orders)\n", + "print(total_products_ordered)\n", + "percentage_ordered = (total_products_ordered / len(products)) * 10\n", + "print(percentage_ordered)\n", + "order_status=(total_products_ordered,percentage_ordered)\n", + "\n", + "# Print statistics\n", + "print(\"Order Statistics:\")\n", + "print(\"Total Products Ordered:\", total_products_ordered)\n", + "print(\"Percentage of Products Ordered:\", percentage_ordered, \"%\")\n", + "\n", + "# Update inventory ------> ANSWER 3:Already set in the previous lab\n", + "for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -=1 \n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2b7e6124-3281-43e5-a167-14576347cc19", + "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 +147,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,