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
177 changes: 176 additions & 1 deletion lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,181 @@
"\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "46851c10-0d40-447e-aadb-43503b228bba",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "745832c9-4f0a-4bce-97d1-645307e69a11",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: 5\n",
"Enter the quantity of mugs available: 4\n",
"Enter the quantity of hats available: 3\n",
"Enter the quantity of books available: 2\n",
"Enter the quantity of keychains available: 1\n"
]
}
],
"source": [
"def initialize_inventory(products):\n",
" inventory = {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n",
" return inventory\n",
"\n",
"inventory = initialize_inventory(products)\n"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "3cc0a5be-2d61-4a45-882a-a2064475b212",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the number of customer orders: 2\n",
"Enter the name of a product that a customer wants to order: hat\n",
"Enter the name of a product that a customer wants to order: keychain\n"
]
}
],
"source": [
"def get_customer_orders():\n",
" orders_number = int(input(\"Enter the number of customer orders: \"))\n",
"\n",
" customer_orders = { input(\"Enter the name of a product that a customer wants to order: \") for i in range(orders_number) }\n",
"\n",
" return customer_orders\n",
"\n",
"\n",
"customer_orders = get_customer_orders()\n",
"\n",
" \n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "eaf27977-b09a-452a-9e50-8e421383510c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics\n",
"Total Products Ordered: 2\n",
"Percentage of Unique Products Ordered: 40.0\n"
]
}
],
"source": [
"total_products_ordered = len(customer_orders)\n",
"percentage_of_products_ordered = (total_products_ordered / len(products))*100\n",
"\n",
"\n",
"print(\"Order Statistics\")\n",
"print(\"Total Products Ordered: \", total_products_ordered)\n",
"print(\"Percentage of Unique Products Ordered: \", percentage_of_products_ordered) "
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "0d53d3d7-615d-48cb-9384-53ce09bf5fcc",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price of keychain : 5\n",
"Enter the price of hat : 10\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total price : 15.0\n"
]
}
],
"source": [
"#Add a new function to calculate the total price of the customer order. \n",
"#For each product in customer_orders, prompt the user to enter the price of that product.\n",
"#Use comprehension to calculate the total price. Note: assume that the user can only have 1 unit of each product.\n",
"\n",
"def total_order_price(customer_orders):\n",
"\n",
" prices = [ float(input(f\"Enter the price of {product} :\")) for product in customer_orders]\n",
"\n",
" total = sum(prices)\n",
"\n",
" return total \n",
"\n",
"prices = total_order_price(customer_orders)\n",
"print(\"Total price : \", prices)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "333cef44-a485-4768-9124-65f8f0720b2b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-shirt: 5\n",
"mug: 4\n",
"hat: 2\n",
"book: 2\n"
]
}
],
"source": [
"#Modify the update_inventory function to remove the product from the inventory if its quantity becomes zero \n",
"#after fulfilling the customer orders.\n",
"#Use comprehension to filter out the products with a quantity of zero from the inventory.\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
"\n",
" updated_inventory = { product : (inventory[product] -1 if product in customer_orders else inventory[product]) for product in inventory}\n",
" updated_inventory_0 = { product : quantity for product, quantity in updated_inventory.items() if quantity > 0}\n",
"\n",
" return updated_inventory_0\n",
" \n",
"inventory = update_inventory(customer_orders, inventory)\n",
"for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3a4e34b7-1c65-4cd1-a5d1-b28aa4a4effe",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -93,7 +268,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down