Skip to content
Open
Show file tree
Hide file tree
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
Binary file added .DS_Store
Binary file not shown.
152 changes: 150 additions & 2 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,159 @@
"\n",
"```\n"
]
},
{
"cell_type": "markdown",
"id": "eace0525",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "c8ecb04b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'total order price is $20'"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"\n",
"\n",
"def initialize_inventory(products):\n",
" inventory= {product: int(input(f'Enter the quantity of \"{product}\" in the inventory: ')) for product in products}\n",
" \n",
" return inventory\n",
"\n",
"inventory=initialize_inventory(products)\n",
"\n",
"# Apply comprehension to get customer order function: \n",
"customer_order=set()\n",
"def get_customer_orders():\n",
" n = int(input(\"How many products would you like to order? \"))\n",
" customer_order={input(\"enter a product you want to order between t-shirt, mug, hat, book, keychain \") for i in range(n)}\n",
"\n",
" return customer_order\n",
"\n",
"#updated_inventory function with comprehension:\n",
"\n",
"def update_inventory(customer_order, inventory):\n",
" updated_inventory={product: qty - (1 if product in customer_order else 0) for product, qty in inventory.items() if qty - (1 if product in customer_order else 0) > 0}\n",
" return f\"the updated inventory is : {updated_inventory}\"\n",
"\n",
"def calculate_order_statistics(customer_order, products):\n",
" totalorder=len(customer_order)\n",
" uniqueproducts=totalorder/len(products)*100\n",
" order_statistics=(totalorder, uniqueproducts)\n",
" return order_statistics \n",
"\n",
"def print_order_statistics(order_statistics):\n",
" print(f\"Total products ordered: {order_statistics[0]}\")\n",
" print(f\"Percentage of unique products ordered: {order_statistics[1]:.2f}%\")\n",
"\n",
"def print_updated_inventory(inventory):\n",
" for products,quantities in inventory.items():\n",
" print(f\"{products}: {quantities}\")\n",
"\n",
"\n",
"# build calculate total price function:\n",
"\n",
"\n",
"def calculate_total_price(customer_order):\n",
" prices = [int(input(f\"enter the price of {product}\")) for product in customer_order]\n",
" total_price=sum(prices)\n",
" return f\"total order price is ${total_price}\"\n"
]
},
{
"cell_type": "code",
"execution_count": 68,
"id": "08f5c7ca",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"inital inventory = {'t-shirt': 1, 'mug': 2, 'hat': 2, 'book': 1, 'keychain': 1}\n",
"\n",
"customer has ordered : {'mug', 't-shirt'}\n",
"\n",
"\n",
" Order Statistics: \n",
"Total products ordered: 2\n",
"Percentage of unique products ordered: 40.00%\n",
"\n",
"Updated inventory :\n",
"t-shirt: 1\n",
"mug: 2\n",
"hat: 2\n",
"book: 1\n",
"keychain: 1\n",
"\n",
"total order price is: \n"
]
},
{
"data": {
"text/plain": [
"'total order price is $30'"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#actual program \n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"] #initial product list\n",
"inventory={}#empty inventory dictionary\n",
"\n",
"\n",
"inventory=initialize_inventory(products)\n",
"print(f\"inital inventory = {inventory}\")\n",
"print(\"\")\n",
"customer_order=get_customer_orders()\n",
"print(f\"customer has ordered : {customer_order}\")\n",
"print(\"\")\n",
"updated_inventory=update_inventory(customer_order, inventory)\n",
"print(\"\")\n",
"print(\" Order Statistics: \")\n",
"order_statistics = calculate_order_statistics(customer_order, products)\n",
"print_order_statistics(order_statistics)\n",
"print(\"\")\n",
"print(\"Updated inventory :\") \n",
"print_updated_inventory(inventory)\n",
"print(\"\")\n",
"print(\"total order price is: \")\n",
"calculate_total_price(customer_order)\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d2852429",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -93,7 +241,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down