Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 110 additions & 2 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"\n",
"1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n",
"\n",
"\n",
"2. Create an empty dictionary called `inventory`.\n",
"\n",
"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.\n",
Expand Down Expand Up @@ -50,11 +51,118 @@
"\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": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"mug is out of stock.\n",
"Customer orders: {'book', 'keychain', 'mug'}\n",
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.0%\n",
"t-shirt: 1\n",
"mug: 0\n",
"hat: 1\n",
"book: 0\n",
"keychain: 0\n"
]
}
],
"source": [
"#1. Define a list called `products` that contains the following items: \n",
"#\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"#2. Create an empty dictionary called `inventory`.\n",
"inventory = {}\n",
"\n",
"#3. Ask the user to input the quantity of each product available in the inventory. \n",
"#Use the product names from the `products` list as keys in the `inventory` dictionary \n",
"#and assign the respective quantities as values.\n",
"for product in products:\n",
" quantity = int(input(f\"Enter the quantity of {product}: \"))\n",
" inventory[product] = quantity\n",
"\n",
"#4. Create an empty set called `customer_orders`.\n",
"\n",
"#Please keep in mind, because we use set, the customer can only order each product once.\n",
"customer_orders = set()\n",
"\n",
"#5. Ask the user to input the name of three products that a customer wants to order \n",
"# (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \n",
"# \"book\" or \"keychain\". Add each product name to the `customer_orders` set.\n",
"y=3\n",
"n = 0\n",
"\n",
"# I will use a placeholder inventory to track the available quantities during the\n",
"# ordering process\n",
"inventory_workingvalue = inventory.copy()\n",
"\n",
"#The while loop bellow would allow the user to buy several products of the same cathegory \n",
"# while it is available in the inventory. But since we are using a set for customer orders,\n",
"# the customer can only order each product once =(.\n",
"\n",
"while n < y:\n",
" order = input(f\"Enter the name of a product you would like to order {n+1}: \") \n",
" if order in products and inventory_workingvalue[order] > 0:\n",
" customer_orders.add(order)\n",
" inventory_workingvalue[order] -= 1\n",
" n+=1\n",
" elif order in products and inventory_workingvalue[order] == 0:\n",
" print(f\"{order} is out of stock.\") \n",
" else:\n",
" print(f\"{order} is not available in the products list.\\nWe only sell t-shirts, mugs, hats, books, keychains\")\n",
" \n",
"#6. Print the products in the `customer_orders` set.\n",
"print(\"Customer orders:\", customer_orders)\n",
"\n",
"#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 \n",
"# the total available products.\n",
" \n",
"# Store these statistics in a tuple called `order_status`.\n",
"total_products_ordered = len(customer_orders)\n",
"percentage_products_ordered = (total_products_ordered / sum(inventory.values())) * 100\n",
"order_status = (total_products_ordered, percentage_products_ordered)\n",
"\n",
"#8. Print the order statistics using the following format:\n",
"# ```\n",
"# Order Statistics:\n",
"# Total Products Ordered: <total_products_ordered>\n",
"# Percentage of Products Ordered: <percentage_ordered>% \n",
"# ```\n",
"print(\"Order Statistics:\")\n",
"print(f\"Total Products Ordered: {total_products_ordered}\")\n",
"print(f\"Percentage of Products Ordered: {percentage_products_ordered}%\")\n",
"\n",
"#9. Update the inventory by subtracting 1 from the quantity of each product. \n",
"# Modify the `inventory` dictionary accordingly.\n",
"\n",
"def inventory_new(inventory):\n",
" for product in customer_orders:\n",
" if product in inventory and inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" return inventory\n",
"\n",
"inventory = inventory_new(inventory) \n",
"\n",
"#10. Print the updated inventory, displaying the quantity of each product on separate lines.\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"
},
Expand All @@ -68,7 +176,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.7"
}
},
"nbformat": 4,
Expand Down