diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..af5aa4e2 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -39,8 +39,7 @@ "\n", "8. Print the order statistics using the following format:\n", " ```\n", - " Order Statistics:\n", - " Total Products Ordered: \n", + "@ Total Products Ordered: \n", " Percentage of Products Ordered: % \n", " ```\n", "\n", @@ -50,13 +49,386 @@ "\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": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + } + ], + "source": [ + "# 1. Define a list called products that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "print(products)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "# 2. Create an empty dictionary called inventory.\n", + "\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity for t-shirt: 5\n", + "Enter the quantity for mug: 6\n", + "Enter the quantity for hat: 4\n", + "Enter the quantity for book: 5\n", + "Enter the quantity for keychain: 3\n" + ] + } + ], + "source": [ + "# 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.\n", + "\n", + "for item in products :\n", + " value = input(f\"Enter the quantity for {item}: \")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(value)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': '2', 'mug': 0, 'hat': -1, 'book': 0, 'keychain': '2'}\n" + ] + } + ], + "source": [ + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [], + "source": [ + "# 4. Create an empty set called customer_orders\n", + "customer_orders = set()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product 1 for the order: hat\n", + "Enter product 2 for the order: mug\n", + "Enter product 3 for the order: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The customer orders are {'hat', 'book', 'mug'}\n" + ] + } + ], + "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.\n", + "\n", + "for i in range(3):\n", + " while True:\n", + " order = input(f\"Enter product {i+1} for the order: \")\n", + " \n", + " if order in products:\n", + " customer_orders.add(order)\n", + " break\n", + " else:\n", + " print(f\"Sorry, '{order}' is not in our product list. Please try again.\")\n", + "\n", + "\n", + "print(f\"The customer orders are {customer_orders}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'book', 'mug'}\n" + ] + } + ], + "source": [ + "# 6. Print the products in the customer_orders set.\n", + "\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "n_custom_o = len(customer_orders)\n", + "print(n_custom_o)" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "# 7. Calculate the following order statistics:\n", + "\n", + "# Total Products Ordered: The total number of products in the `customer_orders` set.\n", + "\n", + "total_products = len(customer_orders)\n", + "print(total_products)" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [ + { + "ename": "AttributeError", + "evalue": "'dict' object has no attribute 'value'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mAttributeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[79]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;66;03m# Percentage of Products Ordered: The percentage of products ordered compared to the total available products\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m total_products = \u001b[38;5;28msum\u001b[39m(\u001b[43minventory\u001b[49m\u001b[43m.\u001b[49m\u001b[43mvalue\u001b[49m())\n\u001b[32m 3\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mTotal:\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mtotal_products\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m\"\u001b[39m)\n\u001b[32m 5\u001b[39m \u001b[38;5;66;03m# DUDA: por que no ha funcionado si todos los values son integers - lo comprobé al principio\u001b[39;00m\n", + "\u001b[31mAttributeError\u001b[39m: 'dict' object has no attribute 'value'" + ] + } + ], + "source": [ + "# Percentage of Products Ordered: The percentage of products ordered compared to the total available products\n", + "total_products = sum(inventory.value())\n", + "print(f\"Total:{total_products}\")\n", + "\n", + "# por que no ha funcionado si todos los values son integers" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# hecho con ayuda de anaconda assistant:" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], + "source": [ + "total_products = sum(int(value) for value in inventory.values() if str(value).isdigit())\n", + "print(total_products)" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "75.0\n" + ] + } + ], + "source": [ + "# 7. Percentage of Products Ordered: The percentage of products ordered compared to the total available products\n", + "\n", + "# > there was not line where the number for each item ordered item was introduced, so I'll assume it was one of each \n", + "\n", + "# products ordered = 3\n", + "\n", + "percentage = (100*n_custom_o/total_products)\n", + "print(percentage)" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the percentage of purchased items is 75.0\n" + ] + } + ], + "source": [ + "print(f\"the percentage of purchased items is {percentage}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3, 4)\n" + ] + } + ], + "source": [ + "# Store these statistics in a tuple called `order_status`.\n", + "\n", + "order_status = (n_custom_o, total_products)\n", + "print(order_status)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [], + "source": [ + "# 8. Print the order statistics using the following format:\n", + "# Total Products Ordered: \n", + "# Percentage of Products Ordered: % " + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Products Ordered: 3\n", + "the percentage of purchased items is 75.0\n" + ] + } + ], + "source": [ + "print(f\"Total Products Ordered: {n_custom_o}\")\n", + "print(f\"the percentage of purchased items is {percentage}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': '2', 'mug': -2, 'hat': -3, 'book': -2, 'keychain': '2'}\n" + ] + } + ], + "source": [ + "# 9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n", + "\n", + "for product in customer_orders:\n", + " inventory[product] -= 1\n", + "print(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "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": { @@ -68,7 +440,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,