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
258 changes: 255 additions & 3 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,265 @@
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "b80a54d8-a482-47ce-954e-1748d88c9e2d",
"metadata": {},
"source": [
"#1. Define a function named initialize_inventory that takes products as a parameter. Inside the function, implement the code for initializing the inventory dictionary using a loop and user input."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "55949a29-aa15-4674-86c5-92e05c96c7dc",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"enter the quantity of glasses you want: 2\n",
"enter the quantity of mug you want: 3\n",
"enter the quantity of pen you want: 4\n",
"enter the quantity of lamp you want: 5\n",
"enter the quantity of book you want: 6\n",
"enter the quantity of hat you want: 7\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'glasses': 2, 'mug': 3, 'pen': 4, 'lamp': 5, 'book': 6, 'hat': 7}\n"
]
}
],
"source": [
"products = [\"glasses\", \"mug\", \"pen\", \"lamp\",\"book\", \"hat\"]\n",
"\n",
"def initialize_inventory (products):\n",
" inventory_dictionary= {}\n",
" for product in products : \n",
" y= int(input(f\"enter the quantity of {product} you want: \"))\n",
" inventory_dictionary[product] = y\n",
" return inventory_dictionary\n",
"\n",
"inventory = initialize_inventory (products)\n",
"print(inventory)"
]
},
{
"cell_type": "markdown",
"id": "53dbc190-a7e0-40c7-8707-dbe77113c0c6",
"metadata": {},
"source": [
"#2. Define a function named get_customer_orders that takes no parameters. Inside the function, implement the code for prompting the user to enter the product names using a loop. The function should return the customer_orders set."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "6351a17c-52d2-4b4b-963f-31aded1bba5a",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"enter a product: mug\n",
"do you want to add another product?(Yes/No): yes\n",
"enter a product: book\n",
"do you want to add another product?(Yes/No): yes\n",
"enter a product: lamp\n",
"do you want to add another product?(Yes/No): yes\n",
"enter a product: hat\n",
"do you want to add another product?(Yes/No): no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'hat', 'book', 'lamp', 'mug'}\n"
]
}
],
"source": [
"def get_customer_orders (): \n",
" customer_orders = set()\n",
" products = input(\"enter a product: \")\n",
" customer_orders.add(products)\n",
" x = input(\"do you want to add another product?(Yes/No): \").lower().strip()\n",
" while x == \"yes\" :\n",
" products = input(\"enter a product: \")\n",
" customer_orders.add(products)\n",
" x = input(\"do you want to add another product?(Yes/No): \").lower().strip()\n",
" return customer_orders \n",
"customer_orders = get_customer_orders ()\n",
"print(customer_orders)"
]
},
{
"cell_type": "markdown",
"id": "c7419cb3-ee07-4c57-8163-8b0a660342fe",
"metadata": {},
"source": [
"#3. Define a function named update_inventory that takes customer_orders and inventory as parameters. Inside the function, implement the code for updating the inventory dictionary based on the customer orders."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "2a83b7a4-ad1d-4651-b908-35a3bb555b9b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'glasses': 2, 'mug': 2, 'pen': 4, 'lamp': 4, 'book': 5, 'hat': 6}\n"
]
}
],
"source": [
"def update_inventory (customer_orders, inventory): \n",
" for order in customer_orders:\n",
" if order in inventory: \n",
" inventory[order]= inventory[order]-1\n",
" return inventory\n",
"inventory = update_inventory (customer_orders, inventory)\n",
"print (inventory)"
]
},
{
"cell_type": "markdown",
"id": "ad0f8501-b3cd-4b74-9843-541f33e834ae",
"metadata": {},
"source": [
"#4. Define a function named calculate_order_statistics that takes customer_orders and products as parameters. Inside the function, implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). The function should return these values."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "e5c610a8-ea44-4612-8552-6ec851f2267a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(4, 66.66666666666666)\n"
]
}
],
"source": [
"total_products_orders = len(customer_orders)\n",
"percentage_of_unique_products_ordered = (len(customer_orders)/len(products))*100\n",
"def calculate_order_statistics (customer_orders, products): \n",
" order_statistics = (len(customer_orders), (len(customer_orders)/len(products))*100)\n",
" return order_statistics\n",
"\n",
"order_statistics = calculate_order_statistics (customer_orders, products)\n",
"print (order_statistics)"
]
},
{
"cell_type": "markdown",
"id": "837842c4-4661-4f87-9d1b-d87591dd9746",
"metadata": {},
"source": [
"#5. Define a function named print_order_statistics that takes order_statistics as a parameter. Inside the function, implement the code for printing the order statistics."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "6c2f2fd0-80d0-4338-92d6-df2f3eeb2ef3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"order_statistics: \n",
"Total products orders: 4\n",
"Percentage of unique products ordered: 66.67 %\n"
]
}
],
"source": [
"def print_order_statistics (order_statistics) : \n",
" print (\"order_statistics: \")\n",
" print(f\"Total products orders: {order_statistics[0]}\")\n",
" print (f\"Percentage of unique products ordered: {order_statistics[1]:.2f} %\") \n",
"print_order_statistics (order_statistics)"
]
},
{
"cell_type": "markdown",
"id": "9f17ab34-9283-473a-b348-9e79f1855233",
"metadata": {},
"source": [
"#6. Define a function named print_updated_inventory that takes inventory as a parameter. Inside the function, implement the code for printing the updated inventory."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "4f431fb3-5d5d-4170-be17-0eb71e43d6f1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated inventory: \n",
"glasses 2\n",
"mug 2\n",
"pen 4\n",
"lamp 4\n",
"book 5\n",
"hat 6\n"
]
}
],
"source": [
"def print_updated_inventory (inventory): \n",
" print (\"Updated inventory: \")\n",
" for key,value in inventory.items():\n",
" print (f\"{key} {value}\")\n",
" \n",
"\n",
"print_updated_inventory (inventory)"
]
},
{
"cell_type": "markdown",
"id": "c9041c54-4f96-451e-9616-15ae56f8a3b9",
"metadata": {},
"source": [
"#7. Call the functions in the appropriate sequence to execute the program and manage customer orders."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2cae16d3-dd27-4a90-a0b5-4ba880a12408",
"metadata": {},
"outputs": [],
"source": [
"OK "
]
}
],
"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 +313,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down