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
111 changes: 109 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,118 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "6c13aa54",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n",
"socks is not a valid product.\n",
"glass is not a valid product.\n",
"Products in the customer orders:\n",
"- hat\n",
"- book\n",
"Updated Inventory: {'t-shirt': 5, 'mug': 5, 'hat': 4, 'book': 4, 'keychain': 5}\n",
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 40.00%\n"
]
}
],
"source": [
"# ## Exercise: Managing Customer Orders\n",
"\n",
"# ## Global variables ## #\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"# ## Functions ## #\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(f\"Enter the quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
" return inventory\n",
"\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" while answer := input(\"Do you want to add a product to your order? (yes/no): \") == \"yes\":\n",
" order = input(f\"Enter the name of product {len(customer_orders) + 1} to order: \")\n",
" if order in products:\n",
" customer_orders.add(order)\n",
" else:\n",
" print(f\"{order} is not a valid product.\")\n",
" return customer_orders\n",
"\n",
"def update_inventory(inventory, customer_order) -> dict:\n",
" for item in customer_order:\n",
" if item in inventory:\n",
" inventory[item] -= 1\n",
" return inventory\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = (total_products_ordered / len(products)) * 100\n",
" return total_products_ordered, percentage_ordered\n",
"\n",
"def print_order_statistics(order_status):\n",
" print(\"Order Statistics:\")\n",
" print(f\"Total Products Ordered: {order_status[0]}\")\n",
" print(f\"Percentage of Products Ordered: {order_status[1]:.2f}%\")\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" for item, quantity in inventory.items():\n",
" print(f\"{item}: {quantity}\")\n",
"\n",
"# ## Code ## #\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"print(inventory)\n",
"\n",
"customer_orders = get_customer_orders()\n",
"\n",
"print(\"Products in the customer orders:\")\n",
"for item in customer_orders:\n",
" print(f\"- {item}\")\n",
"\n",
"upd_inventory = update_inventory(inventory, customer_orders)\n",
"\n",
"print(f\"Updated Inventory: {upd_inventory}\")\n",
"\n",
"order_status = calculate_order_statistics(customer_orders, products)\n",
"\n",
"print_order_statistics(order_status)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d743e6b1",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "051f880b",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +168,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.6"
}
},
"nbformat": 4,
Expand Down