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
71 changes: 69 additions & 2 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,78 @@
"\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "6f0f1189",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of unique products ordered: 60.0\n",
"\n",
"Total Price: 299\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 76\n",
"mug: 20\n",
"book: 70\n",
"keychain: 19\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"def initialize_inventory(products):\n",
" inventory = {product: int(input(f\"Please enter the quantity of the {product} available: \")) for product in products}\n",
" return inventory\n",
"\n",
"def get_customer_orders():\n",
" number_of_orders = int(input(\"Please enter the number of items you want to order: \"))\n",
" customer_orders = {input(\"Please enter the name of the product you want to order: \") for i in range(number_of_orders)}\n",
" return customer_orders\n",
" \n",
"def update_inventory(customer_orders, inventory):\n",
" inventory = {\n",
" product: (quantity -1 if product in customer_orders else quantity) \n",
" for product, quantity in inventory.items() if (quantity -1 if product in customer_orders else quantity) >0 \n",
" } #there is no pop or remove since this creates a whole new dict with the conditions in if\n",
" return inventory\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" products_ordered = len(customer_orders)\n",
" percentage_of_unique_products_ordered = (products_ordered / len(products)) *100\n",
" return products_ordered, percentage_of_unique_products_ordered\n",
"\n",
"def total_order_price(customer_orders):\n",
" order_price = [int(input(f\"Please enter the price of the {product}: \")) for product in customer_orders]\n",
" total_order_cost = sum (order_price)\n",
" return total_order_cost\n",
"\n",
"def print_updated_inventory(inventory):\n",
" [print(f\"{product}: {quantity}\") for product, quantity in inventory.items()]\n",
"\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"products_ordered, percentage_of_unique_products_ordered = calculate_order_statistics(customer_orders, products)\n",
"print (f\"Order Statistics:\\nTotal Products Ordered: {products_ordered}\\nPercentage of unique products ordered: {percentage_of_unique_products_ordered}\")\n",
"total_cost = total_order_price(customer_orders)\n",
"print(f\"\\nTotal Price: {total_cost}\")\n",
"print(\"\\nUpdated Inventory:\")\n",
"print_updated_inventory(inventory)\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -93,7 +160,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down