diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..5d77f764 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,13 +50,245 @@ "\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": 100, + "metadata": {}, + "outputs": [], + "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\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "metadata": {}, + "outputs": [], + "source": [ + "# 2 - Create an empty dictionary called inventory.\n", + "\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the quantity of 't-shirt' available in the inventory: 10\n", + "Please enter the quantity of 'mug' available in the inventory: 10\n", + "Please enter the quantity of 'hat' available in the inventory: 10\n", + "Please enter the quantity of 'book' available in the inventory: 10\n", + "Please enter the quantity of 'keychain' available in the inventory: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + } + ], + "source": [ + "# 3 - Ask the user to input the quantity of each product available in the inventory\n", + "\n", + "for product in products: \n", + " while True:\n", + " quantity = input(f\"Please enter the quantity of '{product}' available in the inventory: \") \n", + " if quantity.isdigit():\n", + " inventory[product] = int(quantity)\n", + " break\n", + " else:\n", + " print(f\"Invalid input for '{product}'. The quantity must be a whole positive number. Try again\")\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": {}, + "outputs": [], + "source": [ + "# 4 - Create an empty set called customer_orders.\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "PRODUCTS LIST: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "From the PRODUCTS LIST, please enter the name of three products that a customer wants to order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product nº 1: mug\n", + "Enter product nº 2: at\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. You must enter a product from the list. Try again.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product nº 2: hat\n", + "Enter product nº 3: book\n" + ] + } + ], + "source": [ + "# 5 - Ask the user to input the name of three products that a customer wants to order\n", + "\n", + "print(\"PRODUCTS LIST: \", products)\n", + "print(\"From the PRODUCTS LIST, please enter the name of three products that a customer wants to order.\") \n", + "\n", + "while len(customer_orders) < 3:\n", + " order = input(f\"Enter product nº {len(customer_orders)+1}: \").strip().lower()\n", + " \n", + " if order in products:\n", + " if order in customer_orders:\n", + " print(f\"You've already entered '{order.upper()}'. Please enter a different product from the list.\")\n", + " else:\n", + " customer_orders.add(order)\n", + " else:\n", + " print(\"Invalid input. You must enter a product from the list. Try again.\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 'book', 'hat'}\n" + ] + } + ], + "source": [ + "# 6 - Print the products in the `customer_orders` set.\n", + "\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60.0%\n" + ] + } + ], + "source": [ + "# 7- Calculate the following order statistics:\n", + " # 7.1 - Total Products Ordered: The total number of products in the `customer_orders` set.\n", + "total_products_ordered = len(customer_orders)\n", + " # 7.2 - Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n", + "percentage_of_products_ordered = (len(customer_orders)/len(products))*100\n", + "print(f\"{percentage_of_products_ordered}%\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0%\n" + ] + } + ], + "source": [ + "# 8 - Print the order statistics:\n", + "print(\"Order Statistics:\")\n", + "print(f\"Total Products Ordered: {total_products_ordered}\")\n", + "print(f\"Percentage of Products Ordered: {percentage_of_products_ordered}%\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 9, 'mug': 9, 'hat': 9, 'book': 9, 'keychain': 9}\n" + ] + } + ], + "source": [ + "# 9 - Update the inventory by subtracting 1 from the quantity of each product. \n", + "for product in inventory:\n", + " inventory[product] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt: 9\n", + "mug: 9\n", + "hat: 9\n", + "book: 9\n", + "keychain: 9\n" + ] + } + ], + "source": [ + "# 10 - Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] } ], "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 +300,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,