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
130 changes: 128 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,137 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "1e7a06eb",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "db5411dd",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products :\n",
" quantity = int(input(f\"Enter quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "75e57b56",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders ():\n",
" customer_orders =set()\n",
" answer =\"yes\"\n",
" \n",
" while answer == \"yes\":\n",
" product = input(\"Enter name of product: \")\n",
" customer_orders.add(product)\n",
" answer = input(\"Do you want to add another product? (yes/no): \")\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "3ddeead8",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" for i in customer_orders:\n",
" if i in inventory:\n",
" inventory[i] -= 1\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "c3e5bb1d",
"metadata": {},
"outputs": [],
"source": [
"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"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "fc1df261",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" total_products_ordered, percentage_ordered = order_statistics\n",
" print(f\"Total Products Ordered: {total_products_ordered}\")\n",
" print(f\"Percentage of Products Ordered: {percentage_ordered}%\")"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "1e58fcc3",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" print(\"\\nUpdated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "d844af1a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 40.0%\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 5\n",
"mug: 7\n",
"hat: 9\n",
"book: 4\n",
"keychain: 7\n"
]
}
],
"source": [
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"update_inventory(customer_orders, inventory)\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory(inventory)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +187,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down