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
169 changes: 167 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,176 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "d273fb4d",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(f\"Enter the initial quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "01b4568d",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders(products):\n",
" customer_orders = set()\n",
" while True:\n",
" order = input(\"enter the name of a product the customer wants : \").lower()\n",
"\n",
" if order in products:\n",
" customer_orders.add(order)\n",
" else:\n",
" print(\"That product does not exist in the store.\")\n",
" another = input(\"Do you want to add another product? (yes/no): \").lower()\n",
"\n",
" if another == \"no\":\n",
" break\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e0fd5ed1",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" else:\n",
" print(f\"Sorry, {product} is out of stock.\")\n",
"\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a484a55a",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" total_customer_orders =len(customer_orders)\n",
" total_products = len(products)\n",
" percentage_order = (total_customer_orders / total_products) * 100\n",
" \n",
" return total_customer_orders, percentage_order\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "607b1cd3",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" total = order_statistics[0]\n",
" percentage = order_statistics[1]\n",
"\n",
" print(\"total orders:\", total)\n",
" print(\"percentage:\", percentage)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "f0cbcb69",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" print(\"Inventory:\")\n",
" for product in inventory:\n",
" print(product, inventory[product])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "df81c1fd",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "f6bccec0",
"metadata": {},
"outputs": [],
"source": [
"customer_orders = get_customer_orders(products)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "bbdf44ff",
"metadata": {},
"outputs": [],
"source": [
"update_inventory(customer_orders, inventory)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "8c945de4",
"metadata": {},
"outputs": [],
"source": [
"order_statistics = calculate_order_statistics(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "190a692a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"total orders: 1\n",
"percentage: 20.0\n",
"Inventory:\n",
"t-shirt 10\n",
"mug 8\n",
"hat 5\n",
"book 12\n",
"keychain 4\n"
]
}
],
"source": [
"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 +226,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down