From af9e8aa65d7206e3a424781f5ced885cb81e6bfe Mon Sep 17 00:00:00 2001 From: JesusB11 Date: Wed, 18 Jun 2025 08:30:20 +0200 Subject: [PATCH] I've completed the lab and made notes on the code --- lab-python-data-structures.ipynb | 212 ++++++++++++++++++++++++++++++- 1 file changed, 210 insertions(+), 2 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..f4b98bfa 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,11 +50,219 @@ "\n", "Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. " ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "quantity_user = \"\"\n", + "questions = 0\n", + "sum_inventory_final = 0\n", + "\n", + "\n", + "while questions < len(products): #just to make sure we are asking for the 5 same products that are in the list.\n", + "\n", + " for product in products: \n", + " print(\"Provide the quantity for\", product) #asking for the imput for each product that goes in the inventory\n", + " quantity_user= input()\n", + " quantity_user_check = quantity_user.isdigit() #checking that it is a number\n", + " if not quantity_user_check or int(quantity_user) <= 0: #if it is not a number or less than 0, we break the loop and print a message\n", + " print(\"Quantity must be a number and greater than 0. Start again\") \n", + " break\n", + " else:\n", + " questions += 1 #if the number is over 0 and the loop continues adding 1 to the questions variable\n", + " # print(questions)\n", + " inventory [product] = int(quantity_user) #adding the product as key and the quantity as value to the dictionary\n", + " sum_inventory_final += int(quantity_user) #adding the quantity to calculate the percentage at the end.\n", + "\n", + "\n", + "customer_orders = set() #creating the set as requested in point 5\n", + "customer_order = (input(\"Enter the three products separated by commas that the customer wants to order from our cataloge:\")) #now asking for the order \n", + "customer_order = customer_order.replace(\" \", \"\") #deleting spaces so it can be split correctly and counted for the inventory\n", + "orders_no_spaces= customer_order.split(\",\") # creating the list dividing the input by commas.\n", + "customer_orders= set(orders_no_spaces) #cAdding the data to the set.\n", + "print(customer_orders) #printing order as requested in point 6\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "total_products_percentage=(len(customer_orders)/sum_inventory_final*100) \n", + "\n", + "order_status = (total_products_ordered, total_products_percentage) #creating the tuple as requested in point 7\n", + "\n", + "\n", + "print(\"Order Statistics:\") #point 8 printing as requested\n", + "print(\"Total products ordered: \", total_products_ordered) #point 7 statistics 1 done\n", + "print(\"Percentage of products ordered: \", total_products_percentage, \"%\") #point 7 statistics 2 done\n", + "\n", + "for order in orders_no_spaces:\n", + " if order in inventory:\n", + " # print(order)\n", + " inventory[order] = inventory[order] -1 #updating the inventory.\n", + "\n", + "\n", + "\n", + "print(\"updated inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"- {product}: {quantity}\")\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mug\n", + "hat\n", + "book\n", + "{'t-shirt': 10, 'mug': 9, 'hat': 9, 'book': 9, 'keychain': 10}\n" + ] + } + ], + "source": [ + "# products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "# inventory = {'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n", + "# orders_set = set['mug', 'hat', 'book'] \n", + "# order_list = ['mug', 'hat', 'book'] #this is just to test the code, it should be removed later\n", + "# # # customer_orders = set(input(\"Enter the three products that the customer wants to order, separated by commas: \"))\n", + "# # customer_orders = input(\"enter:\")\n", + "# # customer_orders = customer_orders.replace(\" \", \"\")\n", + "# # orders_no_spaces= customer_orders.split(\",\") # removing any leading or trailing spaces from the input\n", + "# # print(orders_no_spaces)\n", + "# orders_set= set(orders_no_spaces)\n", + "# print(orders_set)\n", + "# print(inventory[0]) \n", + "\n", + "# print(inventory ['hat'])\n", + "\n", + "\n", + "# customer_orders\n", + "# print((orders_set))\n", + "# print(f\"Total Products Ordered:\", len(orders_set))\n", + "\n", + "# customer_orders = (input(\"Enter the three products that the customer wants to order, separated by commas: \"))\n", + "# customer_orders_lst= [customer_orders.split(\",\")]\n", + "# print(customer_orders_lst)\n", + "# if customer_orders_lst not in products:\n", + "# print(products)\n", + "# print(\"One or more products are not sold by the company. Here's the list of products that we offer: \", products)\n", + "\n", + "# for order in order_list: \n", + "# if order in inventory:\n", + "# print(order)\n", + "# inventory[order] = inventory[order] -1\n", + "\n", + "# print (inventory)\n", + "\n", + "# print(inventory.items())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Provide the quantity for t-shirt\n", + "Quantity must be a number and greater than 0\n", + "Provide the quantity for t-shirt\n", + "Provide the quantity for mug\n", + "Quantity must be a number and greater than 0\n", + "Provide the quantity for mug\n", + "Provide the quantity for hat\n", + "Provide the quantity for book\n", + "Quantity must be a number and greater than 0\n", + "Provide the quantity for book\n", + "Provide the quantity for keychain\n", + "{'1'}\n", + "Order Statistics:\n", + "Total products ordered: 1\n", + "Percentage of products ordered: 20.0 %\n", + "updated inventory:\n", + "- t-shirt: 0\n", + "- mug: 0\n", + "- book: 1\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "quantity_user = \"\"\n", + "questions = 0\n", + "\n", + "\n", + "\n", + "\n", + "while questions < len(products): #just to make sure we are asking for the 5 same products that are in the list.\n", + "\n", + " for product in products: \n", + " print(\"Provide the quantity for\", product) #asking for the imput for each product that goes in the inventory\n", + " quantity_user= input()\n", + " quantity_user_check = quantity_user.isdigit() #checking that it is a number\n", + " if not quantity_user_check or int(quantity_user) <= 0: #if it is not a number or less than 0, we break the loop and print a message\n", + " print(\"Quantity must be a number and greater than 0\")\n", + " print(\"Provide the quantity for\", product) #asking for the imput for each product that goes in the inventory\n", + " quantity_user= input()\n", + " \n", + " # continue\n", + " questions += 1 #if the number is over 0 and the loop continues adding 1 to the questions variable\n", + " # print(questions)\n", + " inventory [product] = int(quantity_user) #adding the product as key and the quantity as value to the dictionary\n", + " # else:\n", + " questions += 1 #if the number is over 0 and the loop continues adding 1 to the questions variable\n", + " # print(questions)\n", + " inventory [product] = int(quantity_user) #adding the product as key and the quantity as value to the dictionary\n", + "\n", + "customer_orders = set()\n", + "customer_order = (input(\"Enter the three products separated by commas that the customer wants to order from our cataloge:\")) #now asking for the order \n", + "customer_order = customer_order.replace(\" \", \"\") #deleting spaces so it can be split correctly and counted for the inventory\n", + "orders_no_spaces= customer_order.split(\",\") # creating the list dividing the input by commas.\n", + "customer_orders= set(orders_no_spaces) #creating the set as requested in point 5\n", + "print(customer_orders)\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "total_products_percentage=(len(customer_orders)/len(products)*100)\n", + "\n", + "\n", + "order_status = (total_products_ordered, total_products_percentage) #creating the tuple as requested in point 7\n", + "\n", + "\n", + "print(\"Order Statistics:\") #point 8 printing as requested\n", + "print(\"Total products ordered: \", total_products_ordered) #point 7 statistics 1 done\n", + "print(\"Percentage of products ordered: \", total_products_percentage, \"%\") #point 7 statistics 2 done\n", + "\n", + "for order in customer_order:\n", + " if order in inventory:\n", + " inventory[order] = inventory[order] -1 #updating the inventory.\n", + "\n", + "\n", + "\n", + "print(\"updated inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"- {product}: {quantity}\")\n", + "\n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -68,7 +276,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.3" } }, "nbformat": 4,