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
272 changes: 269 additions & 3 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,279 @@
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "a9208d96-1019-4d15-b5c7-e21b3b7c5df4",
"metadata": {},
"source": [
"# 1. \n",
"\n",
"def initialize_inventory("
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "247557e1-5f1e-42d3-844c-8232b2d57244",
"metadata": {},
"outputs": [],
"source": [
"# 1.\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" print(\"Enter the quantity available for each product:\")\n",
" \n",
" for product in products:\n",
" quantity = input(f\"Quantity of {product}: \")\n",
" \n",
" while not quantity.isdigit():\n",
" print(\"Please enter a valid number.\")\n",
" quantity = input(f\"Quantity of {product}: \")\n",
" inventory[product] = int(quantity)\n",
" \n",
" return inventory\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "8bb1b471-297a-4f5f-b9b5-16c316ffd89a",
"metadata": {},
"outputs": [],
"source": [
"# 2. \n",
"\n",
"def get_customer_orders(products):\n",
" customer_orders = set()\n",
" print(\"\\nEnter the products that the customer wants to order.\")\n",
" \n",
" while True:\n",
" order = input(\"Enter a product name: \").lower()\n",
" \n",
" while order not in products:\n",
" print(\"Invalid product. Please enter a valid product from the list.\")\n",
" order = input(\"Enter a product name: \").lower()\n",
" customer_orders.add(order)\n",
"\n",
" another = input(\"Add another product? (yes/no): \").lower()\n",
" if another not in (\"yes\", \"y\"):\n",
" break\n",
" \n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a9b51606-1308-4798-a8b1-9c0a4df8d0c2",
"metadata": {},
"outputs": [],
"source": [
"# 3. \n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" for item in customer_orders:\n",
" inventory[item] -= 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fcb5f1cd-223d-4c1f-af5f-a250be27e7f0",
"metadata": {},
"outputs": [],
"source": [
"# 4. \n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" \n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = (total_products_ordered / len(products)) * 100\n",
" \n",
" return total_products_ordered, percentage_ordered"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "53ea0d7a-86ea-4018-97fb-a086a777c2eb",
"metadata": {},
"outputs": [],
"source": [
"# 5. \n",
"\n",
"def print_order_statistics(order_statistics):\n",
" print(\"\\nOrder Statistics:\")\n",
" print(f\"Total Products Ordered: {order_statistics[0]}\")\n",
" print(f\"Percentage of Products Ordered: {order_statistics[1]:.2f}%\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ef5afaba-11ff-47c5-bd49-af1f1f186e9d",
"metadata": {},
"outputs": [],
"source": [
"# 6. \n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(\"\\nUpdated Inventory:\")\n",
" \n",
" for product, qty in inventory.items():\n",
" print(f\"{product}: {qty}\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "498d4712-13c3-438a-9cec-2323726c7723",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the quantity available for each product:\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Quantity of t-shirt: 25\n",
"Quantity of mug: 45\n",
"Quantity of hat: 10\n",
"Quantity of book: 9\n",
"Quantity of keychain: 15\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Enter the products that the customer wants to order.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product name: keychain\n",
"Add another product? (yes/no): y\n",
"Enter a product name: hat\n",
"Add another product? (yes/no): yes\n",
"Enter a product name: book\n",
"Add another product? (yes/no): \n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.00%\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 25\n",
"mug: 45\n",
"hat: 9\n",
"book: 8\n",
"keychain: 14\n"
]
}
],
"source": [
"# 7. Program\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"# 1. Function to initialize inventory\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" print(\"Enter the quantity available for each product:\")\n",
" for product in products:\n",
" quantity = input(f\"Quantity of {product}: \")\n",
" while not quantity.isdigit():\n",
" print(\"Please enter a valid number.\")\n",
" quantity = input(f\"Quantity of {product}: \")\n",
" inventory[product] = int(quantity)\n",
" return inventory\n",
"\n",
"\n",
"# 2. Function to get customer orders\n",
"def get_customer_orders(products):\n",
" customer_orders = set()\n",
" print(\"\\nEnter the products that the customer wants to order.\")\n",
" while True:\n",
" order = input(\"Enter a product name: \").lower()\n",
" while order not in products:\n",
" print(\"Invalid product. Please enter a valid product from the list.\")\n",
" order = input(\"Enter a product name: \").lower()\n",
" customer_orders.add(order)\n",
"\n",
" another = input(\"Add another product? (yes/no): \").lower()\n",
" if another not in (\"yes\", \"y\"):\n",
" break\n",
" return customer_orders\n",
"\n",
"\n",
"# 3. Function to update inventory\n",
"def update_inventory(customer_orders, inventory):\n",
" for item in customer_orders:\n",
" inventory[item] -= 1\n",
"\n",
"\n",
"# 4. Function to calculate statistics\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",
"\n",
"# 5. Function to print statistics\n",
"def print_order_statistics(order_statistics):\n",
" print(\"\\nOrder Statistics:\")\n",
" print(f\"Total Products Ordered: {order_statistics[0]}\")\n",
" print(f\"Percentage of Products Ordered: {order_statistics[1]:.2f}%\")\n",
"\n",
"\n",
"# 6. Function to print updated inventory\n",
"def print_updated_inventory(inventory):\n",
" print(\"\\nUpdated Inventory:\")\n",
" for product, qty in inventory.items():\n",
" print(f\"{product}: {qty}\")\n",
"\n",
"# Functions Call\n",
"\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders(products)\n",
"update_inventory(customer_orders, inventory)\n",
"order_stats = calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(order_stats)\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "268f9b27-9613-477e-aca6-d333cf3fb378",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -61,7 +327,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down