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
162 changes: 160 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,169 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "bf334877",
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"customer_orders = set()\n",
"\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "640786d3",
"metadata": {},
"outputs": [],
"source": [
"# 1\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(f\"Enter a quantity of {product}: \"))\n",
" inventory[product] = quantity\n",
"\n",
" return inventory\n",
"\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "02abdad2",
"metadata": {},
"outputs": [],
"source": [
"#2\n",
"\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
"\n",
" while True:\n",
" order = input(\"Enter a product to order or 'exit' to finish: \")\n",
" if order == \"exit\":\n",
" break\n",
" else:\n",
" customer_orders.add(order)\n",
" return customer_orders\n",
"\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "45edb707",
"metadata": {},
"outputs": [],
"source": [
"# 3\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" for order in customer_orders:\n",
" if order in inventory and inventory[order] > 0:\n",
" inventory[order] -=1\n",
"\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "da97a55d",
"metadata": {},
"outputs": [],
"source": [
"#4\n",
"\n",
"def calculate_order_statistics (customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage = (total_products_ordered / len(products)) * 100\n",
"\n",
" return total_products_ordered, percentage\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "2b423687",
"metadata": {},
"outputs": [],
"source": [
"# 5\n",
"\n",
"def print_order_statistics (order_statistics):\n",
" total_products_ordered, percentage = order_statistics\n",
"\n",
" print(f\"Total products ordered: {total_products_ordered}\")\n",
" print(f\"Percentage of products ordered: {percentage:.2f}\")\n"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "8c59886d",
"metadata": {},
"outputs": [],
"source": [
"#6 \n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\") \n"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "3f5567cd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total products ordered: 3\n",
"Percentage of products ordered: 60.00\n",
"Updated Inventory:\n",
"t-shirt: 5\n",
"mug: 5\n",
"hat: 3\n",
"book: 2\n",
"keychain: 21\n"
]
}
],
"source": [
"# 7\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",
"print_order_statistics (order_statistics) \n",
"print_updated_inventory (updated_inventory)\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +219,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down