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-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,132 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ec6d83b3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Order Statistics:\n",
"Total products ordered: 3\n",
"Percentage of unique products ordered: 75.00%\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 245\n",
"mug: 31\n",
"hat: 11\n",
"hoodie: 15\n"
]
}
],
"source": [
"def initialize_inventory(products):\n",
" \"\"\"\n",
" Creates an inventory dictionary using user input for quantities.\n",
" \"\"\"\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(f\"Enter the quantity of {product}: \"))\n",
" inventory[product] = quantity\n",
" return inventory\n",
"\n",
"\n",
"def get_customer_orders():\n",
" \"\"\"\n",
" Prompts the user to enter product names for the order.\n",
" Returns a set of customer orders.\n",
" \"\"\"\n",
" customer_orders = set()\n",
"\n",
" while True:\n",
" product = input(\"Enter the name of a product that a customer wants to order: \").strip().lower()\n",
" customer_orders.add(product)\n",
"\n",
" another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n",
" if another != \"yes\":\n",
" break\n",
"\n",
" return customer_orders\n",
"\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" \"\"\"\n",
" Updates the inventory by subtracting 1 for each ordered product\n",
" (only if the product exists in inventory).\n",
" \"\"\"\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1\n",
" return inventory\n",
"\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" \"\"\"\n",
" Calculates:\n",
" - total products ordered (size of customer_orders set)\n",
" - percentage of unique products ordered (vs available products)\n",
" Returns a tuple: (total_ordered, percent_unique_ordered)\n",
" \"\"\"\n",
" total_ordered = len(customer_orders)\n",
" percent_unique_ordered = (total_ordered / len(products)) * 100 if len(products) > 0 else 0\n",
" return total_ordered, percent_unique_ordered\n",
"\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" \"\"\"\n",
" Prints the order statistics.\n",
" \"\"\"\n",
" total_ordered, percent_unique_ordered = order_statistics\n",
" print(\"\\nOrder Statistics:\")\n",
" print(f\"Total products ordered: {total_ordered}\")\n",
" print(f\"Percentage of unique products ordered: {percent_unique_ordered:.2f}%\")\n",
"\n",
"\n",
"def print_updated_inventory(inventory):\n",
" \"\"\"\n",
" Prints the updated inventory.\n",
" \"\"\"\n",
" print(\"\\nUpdated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"\n",
"# -------------------------------\n",
"# Main program flow (Step 7)\n",
"# -------------------------------\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"hoodie\"]\n",
"\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory(inventory)\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "c5bdf6e0",
"metadata": {},
"source": [
"git add .\n",
"git commit -m \"Solved lab\"\n",
"git push origin master\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +182,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down