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
74 changes: 71 additions & 3 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,81 @@
"\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "e491f058-34ab-484c-b929-e2ff01e8b668",
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "unmatched ')' (650447696.py, line 18)",
"output_type": "error",
"traceback": [
"\u001b[1;36m Cell \u001b[1;32mIn[11], line 18\u001b[1;36m\u001b[0m\n\u001b[1;33m total_price = sum(int(input(f\"Enter the price of {product}: \"))) for product in customer_orders)\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m unmatched ')'\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {product: int(input(f\"Enter the quantity of {product} available: \")) for product in products}\n",
" return inventory\n",
"\n",
"def get_customer_orders():\n",
" num_orders = int(input(\"How many products would you like to order?\"))\n",
" customer_orders = {input(f\"Enter product name #{i + 1}: \") for i in range(num_orders)}\n",
" return customer_orders\n",
"\n",
"def update_inventory(customer_orders,inventory):\n",
" inventory = {order: inventory[order] - 1 if order in customer_orders else inventory[order] for order in inventory}\n",
" inventory = {order: quantity for order, quantity in inventory.items() if quantity > 0}\n",
" return inventory\n",
"\n",
"def total_price(customer_orders):\n",
" total_price = sum(int(input(f\"Enter the price of {product}: \"))) for product in customer_orders)\n",
" return total_price\n",
" \n",
"def calculate_order_statistics(customer_orders,products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = len(customer_orders)/len(products)*100\n",
" return total_products_ordered, percentage_ordered\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" print(\"Order Statistics: \")\n",
" print(\"Total Products Ordered:\", order_statistics[0])\n",
" print(\"Percentage of Unique Products Ordered:\", order_statistics[1]) \n",
"\n",
"def print_updated_inventory(update_inventory):\n",
" print(f\"The inventory is now: {update_inventory}.\")\n",
" \n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"updated_inventory = update_inventory(customer_orders,inventory)\n",
"order_statistics = calculate_order_statistics(customer_orders,products)\n",
"total = total_price(customer_orders)\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory(updated_inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9810e740-a15b-4a1e-8a5e-6db469866736",
"metadata": {},
"outputs": [],
"source": [
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -93,7 +161,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down