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
136 changes: 134 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
"# Lab | Functions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "35219029",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "0c581062-8967-4d93-b06e-62833222f930",
Expand Down Expand Up @@ -43,11 +51,135 @@
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "241038f5",
"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.\n",
"\n",
"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.\n",
"\n",
"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.\n",
"\n",
"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.\n",
"\n",
"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.\n",
"\n",
"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.\n",
"\n",
"7. Call the functions in the appropriate sequence to execute the program and manage customer orders."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "ad0fd02b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initial Inventory:\n",
"{'t-shirt': 10, 'mug': 20, 'hat': 30, 'book': 40, 'keychain': 50}\n",
"\n",
"Order Statistics:\n",
"[5, 5, 100.0]\n",
"\n",
"Updated Inventory:\n",
"t-shirt 9\n",
"mug 18\n",
"hat 29\n",
"book 39\n",
"keychain 50\n"
]
}
],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
" product_count = len(products)\n",
"\n",
" for i in range(product_count):\n",
" quantity = int(input(f\"Enter the quantity for {products[i]} \"))\n",
" inventory[products[i]] = quantity\n",
" return inventory\n",
"\n",
"def get_customer_orders():\n",
" customer_response = True\n",
" dict_cust_order = {\n",
" \"t-shirt\" : 0,\n",
" \"mug\" : 0,\n",
" \"hat\" : 0,\n",
" \"book\" : 0,\n",
" \"keychain\" : 0\n",
" }\n",
"\n",
" while customer_response == True:\n",
" customer_response = int(input(\"Do you want to order more items:(Enter 1 for yes 0 for No): \"))\n",
" if customer_response == True:\n",
" product = input(\"Please give the product to order: \")\n",
"\n",
" if product in inventory:\n",
" dict_cust_order[product] += 1\n",
" \n",
" else:\n",
" print(\"You have entered wrong product\")\n",
" print(\"Please select the product from this list:\", products)\n",
"\n",
" return dict_cust_order\n",
"\n",
"\n",
"def update_inventory(customer_orders,inventory):\n",
" for i,j in inventory.items():\n",
" for a,b in customer_orders.items():\n",
" if i == a:\n",
" inventory[i] = j - b\n",
" return inventory\n",
"\n",
"def calculate_order_statistics(customer_orders,products):\n",
" total_products_ordered = len(customer_orders)\n",
" total_products = len(products)\n",
" percentage_ordered = (100*total_products_ordered)/total_products\n",
"\n",
" order_statistics = [total_products,total_products_ordered,percentage_ordered]\n",
" return order_statistics\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" print(\"\\nOrder Statistics:\")\n",
" print(order_statistics)\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(\"\\nUpdated Inventory:\")\n",
" for item, value in inventory.items():\n",
" print(item, value)\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = initialize_inventory(products)\n",
"print(\"Initial Inventory:\")\n",
"print(inventory)\n",
"customer_orders= get_customer_orders()\n",
"inventory= update_inventory(customer_orders,inventory)\n",
"\n",
"order_statistics=calculate_order_statistics(customer_orders,products)\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory(inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6b96bbc6",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +193,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.2"
}
},
"nbformat": 4,
Expand Down