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
125 changes: 123 additions & 2 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,132 @@
"\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "7f2c4596",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 5, 'mug': 4, 'hat': 3, 'book': 2, 'keychain': 1}"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"products=[\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n",
"inventory={product:int(input(f\"Enter the quantity of the {product} available:\")) for product in products}\n",
"inventory"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "4be4411c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'hat', 'keychain'}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"num_orders = int(input(\"Enter the number of customer orders: \"))\n",
"customer_orders={input(f\"Enter the name of a product that a customer want to order:\") for i in range(num_orders) }\n",
"\n",
"customer_orders"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "024ed2c5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{15.0}"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"total_price=(sum(float(input(f\"Enter the price of {product}: \")) for product in customer_orders))\n",
"total_price\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "e154366e",
"metadata": {},
"outputs": [],
"source": [
"updated_inventory = {product:quantity - (1 if product in customer_orders else 0) for product, quantity in inventory.items() if quantity - (1 if product in customer_orders else 0) > 0}\n"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "39ae7cd5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 5, 'mug': 4, 'hat': 2, 'book': 2}"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"updated_inventory"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "9c26f8f3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total price: {15.0}\n"
]
}
],
"source": [
"print(f\"Total price: {total_price}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -93,7 +214,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.11.9"
}
},
"nbformat": 4,
Expand Down