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
181 changes: 178 additions & 3 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,188 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "01b84b29-3909-45c9-b7ce-b379f362b632",
"metadata": {},
"outputs": [],
"source": [
"#1. Define a function named initialize_inventory that takes products as a parameter. \n",
"#Inside the function, implement the code for initializing the inventory dictionary using \n",
"#a loop and user input.\n",
"\n",
"def initialize_inventory(products):\n",
" inventory={}\n",
" products=products.split(\",\")\n",
" \n",
" n=0\n",
" for prod in products:\n",
" products[n]=prod.strip()\n",
" n+=1\n",
" \n",
" for item in products: \n",
" quant=int(input(f\"Enter the quantity of the product {item}, only integers allowed\"))\n",
" inventory[item]=quant\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "bee86f98-08f5-4c15-9f4c-750db825ef21",
"metadata": {},
"outputs": [],
"source": [
"#2. Define a function named get_customer_orders that takes no parameters. Inside the function, \n",
"#implement the code for prompting the user to enter the product names using a loop. The function \n",
"#should return the customer_orders set.\n",
"\n",
"def get_customer_orders():\n",
" add=1\n",
" customer_orders=set()\n",
" while add==1:\n",
" order_item=input(f\"The producst we have are : {\" \".join(list(inventory.keys()))}.\\nEnter the name of the product you want:\")\n",
" customer_orders.add(order_item)\n",
" more=1\n",
" while more==1:\n",
" add_item=input(\"Do you want add more items? answer yes/no:\")\n",
" if add_item==\"yes\":\n",
" more=0\n",
" add=1\n",
" elif add_item==\"no\":\n",
" add=0\n",
" more=0\n",
" else:\n",
" more=1\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "1ef07739-aa10-48d2-8f05-ce6a6bdf061a",
"metadata": {},
"outputs": [],
"source": [
"#3. Define a function named update_inventory that takes customer_orders and inventory as parameters. \n",
"#Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n",
"\n",
"def update_inventory(customer_orders,inventory):\n",
" for i in customer_orders:\n",
" inventory[i]-=1\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "e60a206d-7ff2-4a37-b1ff-8e3c218803a8",
"metadata": {},
"outputs": [],
"source": [
"# 4.Define a function named calculate_order_statistics that takes customer_orders and products as parameters. \n",
"#Inside the function, implement the code for calculating the order statistics (total products ordered, and \n",
"#percentage of unique products ordered). The function should return these values.\n",
"\n",
"def calculate_order_statistics(customer_orders,products):\n",
" products= inventory.values()\n",
" total_products_ordered= len(customer_orders)\n",
" count=0\n",
" for x in products:\n",
" count+=x\n",
" \n",
" perc_prod_ordered= (total_products_ordered/count)*100\n",
" return total_products_ordered, perc_prod_ordered"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ad22c752-51d9-4413-b342-bf66ee72ff48",
"metadata": {},
"outputs": [],
"source": [
"# 5. Define a function named print_order_statistics that takes order_statistics as a parameter. \n",
"#Inside the function, implement the code for printing the order statistics.\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" x=order_statistics[0]\n",
" y=order_statistics[1]\n",
" return f\"the total of products ordered is :{x}, and the percentage of unique products ordered is; {y}\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "65a43291-0b01-4ada-b6c9-e81a240c6649",
"metadata": {},
"outputs": [],
"source": [
"# 6. Define a function named `print_updated_inventory` that takes `inventory` as a parameter. \n",
"#Inside the function, implement the code for printing the updated inventory.\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(f\"the updated inventory is {inventory}\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "c15f48c2-5950-46e0-bb98-94a5536a6ab5",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of the product hat, only integers allowed 1\n",
"Enter the quantity of the product t-shirt, only integers allowed 4\n",
"Enter the quantity of the product book, only integers allowed 2\n",
"Enter the quantity of the product keyring, only integers allowed 3\n",
"The producst we have are : hat t-shirt book keyring.\n",
"Enter the name of the product you want: hat\n",
"Do you want add more items? answer yes/no: yes\n",
"The producst we have are : hat t-shirt book keyring.\n",
"Enter the name of the product you want: keyring\n",
"Do you want add more items? answer yes/no: no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"the updated inventory is {'hat': 0, 't-shirt': 4, 'book': 2, 'keyring': 2}\n"
]
}
],
"source": [
"# 7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"\n",
"products = \"hat, t-shirt, book, keyring\"\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"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(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ef2e4db9-6fd9-40ea-9562-dec53541511f",
"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 +236,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down