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
243 changes: 241 additions & 2 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
{
"cell_type": "markdown",
"id": "5d500160-2fb7-4777-b5e4-09d45ebaf328",
"id": "f4735399-1e52-4684-9263-6a6e372ddb30",
"metadata": {},
"source": [
"In the previous exercise, you developed a program to manage customer orders and inventory. Now, let's take it a step further and incorporate comprehension into your code.\n",
Expand Down Expand Up @@ -75,6 +75,245 @@
"\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "32a09ad5-391d-4e5f-8faf-90917cd20db4",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 97,
"id": "3abb2f5c-b8d4-421e-8267-4ddf91e97086",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter quantity of bags available: 20\n",
"Enter quantity of shoes available: 30\n"
]
}
],
"source": [
"def initialize_inventory(products):\n",
" inventory = {i: int(input(f\"Enter quantity of {i}s available: \")) for i in products}\n",
" return inventory\n",
"inventory=initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "0b338688-f62f-429b-b5d4-56f996367f63",
"metadata": {},
"outputs": [],
"source": [
"products=[\"bag\",\"shoe\"]"
]
},
{
"cell_type": "code",
"execution_count": 187,
"id": "ecc3f083-05a3-46aa-984a-2180d4ef1af8",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter no of customer orders: 1\n",
"Enter the name of a product that a customer wants to order: mug\n"
]
}
],
"source": [
" def get_customer_orders():\n",
" customer_orders = list()\n",
" number_of_orders=int(input(\"Enter no of customer orders: \"))\n",
" for i in range(number_of_orders):\n",
" name = input(\"Enter the name of a product that a customer wants to order: \")\n",
" customer_orders.append(name)\n",
" order={i for i in customer_orders if i in products}\n",
" return order\n",
" \n",
"customer_order= list(get_customer_orders())\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 181,
"id": "184d8e83-fc03-48fd-a528-0d0671dc1f0a",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the product name or 'complete' to finish: shoe\n",
"do you want to enter another product (yes/no): yes\n",
"Enter the product name or 'complete' to finish: bag\n",
"do you want to enter another product (yes/no): no\n"
]
},
{
"data": {
"text/plain": [
"{'bag', 'shoe'}"
]
},
"execution_count": 181,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6d616f01-f0d2-4b06-aa37-3aa515aafb95",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 178,
"id": "fcd551a7-78f4-4419-a25a-37e0b78fc561",
"metadata": {},
"outputs": [],
"source": [
"def total_price():\n",
" order_dict={i:int(input(f\"Price of {i}: \")) for i in customer_order}\n",
" order_dict.items()\n",
" b=sum(order_dict.values())\n",
" print(f\"The total product price is {b}\")"
]
},
{
"cell_type": "code",
"execution_count": 179,
"id": "a0ed8738-8b6e-42bd-9557-0e369346cc29",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Price of shoe: 20\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The total product price is 20\n"
]
}
],
"source": [
"total_price()"
]
},
{
"cell_type": "code",
"execution_count": 80,
"id": "35fa7852-c65c-47f9-9fe0-0dbb60ab1b76",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"function"
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(total_price)\n"
]
},
{
"cell_type": "code",
"execution_count": 175,
"id": "abe71b65-ad08-450a-bffe-b2f62ab716be",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_order, inventory):\n",
" for i in customer_order:\n",
" if i in inventory:\n",
" inventory[i]-=1 \n",
" inventory_final={j:k for j,k in inventory.items() if inventory[j]>0}\n",
" return inventory_final\n",
"customer_order=update_inventory(customer_order, inventory)\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 169,
"id": "8ad47809-0c78-4352-a6f8-1193a5f1a70f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'shoe': 3}"
]
},
"execution_count": 169,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "code",
"execution_count": 177,
"id": "508895fb-4e1b-4fcd-b6d3-145593687082",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Price of shoe: 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The total product price is 2\n"
]
}
],
"source": [
"total_price()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "994641c0-4be7-4400-b8ea-7f2897653d9d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -93,7 +332,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down