diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..4ffdb47 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -7,7 +7,8 @@ "tags": [] }, "source": [ - "# Lab | Flow Control" + "# Lab 02 | Flow Control\n", + "------" ] }, { @@ -37,11 +38,472 @@ "\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": "markdown", + "id": "53cb230b", + "metadata": {}, + "source": [ + "# Solved Lab 01 | Data Structures\n", + "----" + ] + }, + { + "cell_type": "markdown", + "id": "58c41def", + "metadata": {}, + "source": [ + "1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a8634fe2", + "metadata": {}, + "outputs": [], + "source": [ + "# 1.defining the list products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(products)" + ] + }, + { + "cell_type": "markdown", + "id": "59dc1112", + "metadata": {}, + "source": [ + "2. Create an empty dictionary called `inventory`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b9ab03d6", + "metadata": {}, + "outputs": [], + "source": [ + "# 2. creating an empty dictionary invetory\n", + "inventory = {}\n", + "print(inventory)" + ] + }, + { + "cell_type": "markdown", + "id": "64082237", + "metadata": {}, + "source": [ + "3. Ask the user to input the quantity of each product available in the inventory. Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e671b805", + "metadata": {}, + "outputs": [], + "source": [ + "# 3. input of the user for quantity of each product\n", + "for i in range(len(products)):\n", + " while True: # keep asking until valid\n", + " quantity = input(\"Enter the quantity for \" + products[i] + \": \")\n", + " if quantity.isdigit(): # checks if input is a positive integer\n", + " quantity = int(quantity)\n", + " inventory[products[i]] = quantity\n", + " break # valid → exit loop\n", + " else:\n", + " print(f\"❌ Invalid input! Please enter a numeric quantity for {products[i]}.\")\n", + "\n", + "# 3.5 checking our dictionary inventory\n", + "print(inventory)" + ] + }, + { + "cell_type": "markdown", + "id": "17d93e50", + "metadata": {}, + "source": [ + "4. Create an empty set called `customer_orders`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d394e871", + "metadata": {}, + "outputs": [], + "source": [ + "# 4. creating an empty set customer orders\n", + "customer_orders = set()\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "markdown", + "id": "0052d35a", + "metadata": {}, + "source": [ + "5. Ask the user to input the name of three products that a customer wants to order (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the `customer_orders` set." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "812d4117", + "metadata": {}, + "outputs": [], + "source": [ + "# 5. User input for 3 products to order\n", + "print(\"Here is the list of products:\")\n", + "print(products)\n", + "\n", + "print(\"Enter the names of 3 products to order:\")\n", + "\n", + "while len(customer_orders) < 3:\n", + " item = input(\"Enter a product name: \").lower()\n", + "\n", + " if item in products:\n", + " customer_orders.add(item)\n", + " print(f\"✅ Added '{item}' to customer orders.\")\n", + " else:\n", + " print(\"❌ Invalid product! Please choose from:\", products)" + ] + }, + { + "cell_type": "markdown", + "id": "ba960ce5", + "metadata": {}, + "source": [ + "6. Print the products in the `customer_orders` set." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "09802680", + "metadata": {}, + "outputs": [], + "source": [ + "# 6. Printing the ordered products\n", + "print(\"Customer Orders:\", customer_orders)" + ] + }, + { + "cell_type": "markdown", + "id": "839e06ef", + "metadata": {}, + "source": [ + "7. Calculate the following order statistics:\n", + " - Total Products Ordered: The total number of products in the `customer_orders` set.\n", + " - Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n", + " \n", + " Store these statistics in a tuple called `order_status`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6701d3bb", + "metadata": {}, + "outputs": [], + "source": [ + "# 7. Calculating order statistics\n", + "\n", + "# Total Products Ordered\n", + "total_products_ordered = len(customer_orders)\n", + "\n", + "# Percentage of Products Ordered\n", + "percentage_products_ordered = (total_products_ordered / len(products)) * 100\n", + "\n", + "# Calculate Order Status\n", + "order_status = (total_products_ordered, percentage_products_ordered)\n", + "# Print Order Status\n", + "#print(\"Order Status (Total, Percentage):\", order_status)" + ] + }, + { + "cell_type": "markdown", + "id": "9f90868e", + "metadata": {}, + "source": [ + "8. Print the order statistics using the following format:\n", + " ```\n", + " Order Statistics:\n", + " Total Products Ordered: \n", + " Percentage of Products Ordered: % \n", + " ```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6afe8e7c", + "metadata": {}, + "outputs": [], + "source": [ + "# 8. Storing the statistics in a tuple order_status\n", + "print(\"Order Statistics:\")\n", + "print(\"Total Products Ordered:\", total_products_ordered)\n", + "print(f\"Percentage of Products Ordered: {int(percentage_products_ordered)}%\")" + ] + }, + { + "cell_type": "markdown", + "id": "c832a5ab", + "metadata": {}, + "source": [ + "9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ef0a1e9b", + "metadata": {}, + "outputs": [], + "source": [ + "# 9. Updating the inventory by subtracting 1 for each ordered product\n", + "for ordered_product in customer_orders:\n", + " if inventory[ordered_product] > 0:\n", + " inventory[ordered_product] = inventory[ordered_product] - 1" + ] + }, + { + "cell_type": "markdown", + "id": "1705389c", + "metadata": {}, + "source": [ + "10. Print the updated inventory, displaying the quantity of each product on separate lines." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e29f9b9a", + "metadata": {}, + "outputs": [], + "source": [ + "# 10. Printing the updated inventory\n", + "print(\"Updated Inventory :\")\n", + "for product, quantity in inventory.items():\n", + " print(product, \":\", quantity)" + ] + }, + { + "cell_type": "markdown", + "id": "71fc2ec1", + "metadata": {}, + "source": [ + "# Solved Lab 02 | Flow Control\n", + "-----" + ] + }, + { + "cell_type": "markdown", + "id": "d9adb3f0", + "metadata": {}, + "source": [ + "\n", + " 1. Look at your code from the lab data structures, and improve repeated code with loops." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "73a0a88d", + "metadata": {}, + "outputs": [], + "source": [ + "#1. Looking at my code from lab data structures, and improving repeated code with loops:\n", + "# Define the list of products \n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(products)\n", + "# Create an empty dictionary to store inventory\n", + "inventory = {}" + ] + }, + { + "cell_type": "markdown", + "id": "2060afb5", + "metadata": {}, + "source": [ + "------\n", + " 2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:" + ] + }, + { + "cell_type": "markdown", + "id": "832d175c", + "metadata": {}, + "source": [ + " 2.a. Prompt the user to enter the name of a product that a customer wants to order." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "323ed1d8", + "metadata": {}, + "outputs": [], + "source": [ + "# Define the list of products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(\"Available products:\", products)\n", + "\n", + "# Create an empty dictionary to store inventory\n", + "inventory = {}\n", + "\n", + "while True:\n", + " product_name = input(\"Enter the name of the product you want to order: \").lower().strip()\n", + " if product_name in products:\n", + " # Add product to inventory or increase quantity\n", + " if product_name in inventory:\n", + " inventory[product_name] += 1\n", + " else:\n", + " inventory[product_name] = 1\n", + "\n", + " print(f\"You have added the '{product_name}' to inventory.\")\n", + " break # exit loop after successful entry\n", + " else:\n", + " print(\"Product not found. Please choose from:\", products)\n", + "\n", + "print(\"\\nFinal inventory:\", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8485511b", + "metadata": {}, + "outputs": [], + "source": [ + "#2.a. Prompt user to enter the name of a product that a customer wants to order:\n", + "print(f\"Available porducts:{products}\")\n", + "product_name = input(\"Enter the name of the product you want to order: \").lower()\n", + "print(\"Product added to order:\", product_name)" + ] + }, + { + "cell_type": "markdown", + "id": "adbc0f3d", + "metadata": {}, + "source": [ + " 2.b. Add the product name to the \"customer_orders\" set." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ae7a7caf", + "metadata": {}, + "outputs": [], + "source": [ + "#2.b. Add the product name to the \"customer_orders\" set:\n", + "customer_orders = set()\n", + "customer_orders.add(product_name)\n", + "print(\"Customer Orders:\", customer_orders)" + ] + }, + { + "cell_type": "markdown", + "id": "8e7e7ad2", + "metadata": {}, + "source": [ + " 2.c. Ask the user if they want to add another product (yes/no)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "075b9530", + "metadata": {}, + "outputs": [], + "source": [ + "#2.c. Ask user if they want to add another product (YES/NO):\n", + "answer = input(\"Do you want to add another product? (yes/no): \").lower()\n", + "\n", + "if answer == \"yes\":\n", + " print(f\"Your answer was: {answer}\")\n", + " product_name = input(\"Enter the name of the product you want to order: \").lower()\n", + " if product_name in products:\n", + " print(\"Product added to order:\", product_name)\n", + " customer_orders.add(product_name)\n", + " else:\n", + " print(\"Invalid product name.\")\n", + "\n", + "elif answer == 'no':\n", + " print(f\"Your answer was: {answer}\")\n", + " print(\"No more products to add.\")\n", + "\n", + "else:\n", + " print(f\"Your answer was: {answer}\")\n", + " print(\"Error, Please answer with YES or NO!\")" + ] + }, + { + "cell_type": "markdown", + "id": "527b01bf", + "metadata": {}, + "source": [ + " 2.d. Continue the loop until the user does not want to add another product." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ba84b476", + "metadata": {}, + "outputs": [], + "source": [ + "#2.d. Loop until the user decides to stop adding products:\n", + "while True:\n", + " answer = input(\"Do you want to add another product? (yes/no): \").lower()\n", + "\n", + " if answer == \"yes\":\n", + " product_name = input(\"Enter the name of the product you want to order: \").lower()\n", + "\n", + " if product_name in products:\n", + " print(\"Product added to order:\", product_name)\n", + " customer_orders.add(product_name)\n", + " else:\n", + " print(\"Invalid product name!\")\n", + "\n", + " elif answer == \"no\":\n", + " print(\"No more products to add ~\")\n", + " break # <-- stop the loop\n", + "\n", + " else:\n", + " print(\"Error, please answer with YES or NO!\")" + ] + }, + { + "cell_type": "markdown", + "id": "47479c66", + "metadata": {}, + "source": [ + "------\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": null, + "id": "c54d7740", + "metadata": {}, + "outputs": [], + "source": [ + "#3. Update inventory for ordered products only\n", + "for product in list(customer_orders):\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + " if inventory[product] == 0:\n", + " del inventory[product]" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -55,7 +517,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,