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
70 changes: 68 additions & 2 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,77 @@
"\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3cf16bcc",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"#{key: value for item in iterable} \n",
"#You are constructing the entire dictionary in one expression, not performing assignments.\n",
"#So instead of “assign this value to this key,” you’re defining pairs of key and value.\n",
"def initialize_inventory (products):\n",
" inventory = {product: int(input(f\"Insert Quantity of {product}s\")) for product in products}\n",
" \n",
" return inventory\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"#{value for item in iterable}\n",
"def get_customer_orders():\n",
" num_orders = int(input(f\"How many unique products from the products list do you want?\"))\n",
"\n",
" customer_orders = {input(f\"Select a product from the products list: \").lower() for amount in range(num_orders)}\n",
" \n",
" return customer_orders\n",
"\n",
"customer_orders = get_customer_orders()\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" inventory = {item: inventory[item] - 1 for item in customer_orders if inventory[item] > 1}\n",
"\n",
" return inventory\n",
" \n",
"inventory = update_inventory(customer_orders, inventory)\n",
"\n",
"def calculate_total_price_customer_orders(inventory):\n",
" total_price_customer_orders = {item: float(input(f\"Insert the price of the {item}: \")) for item in inventory}\n",
" total_price_customer_orders = sum(total_price_customer_orders.values())\n",
" print(f\"The total price is: {total_price_customer_orders}\")\n",
" return total_price_customer_orders\n",
"\n",
"total_price = calculate_total_price_customer_orders(inventory)\n",
"\n",
"def calculate_order_statistics(inventory, products):\n",
" total_products_ordered = len(inventory)\n",
" total_available_products = len(products)\n",
" percentage_ordered = (total_products_ordered / total_available_products) * 100\n",
"\n",
" return total_products_ordered, percentage_ordered\n",
"\n",
"order_statistics = calculate_order_statistics(inventory, products)\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" total_products_ordered, percentage_ordered = order_statistics\n",
" print(\"Total unique products ordered:\", total_products_ordered)\n",
" print(\"Percentage of products ordered:\", percentage_ordered, \"%\")\n",
"\n",
"print_order_statistics(order_statistics)\n",
"\n",
"def print_updated_inventory(inventory):\n",
" {print(f\"{product}s: {quantity}\") for product, quantity in inventory.items()}\n",
"\n",
"print_updated_inventory(inventory)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -93,7 +159,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down