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
243 changes: 241 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"# Lab | Functions"
]
},
{
"cell_type": "markdown",
"id": "d249c866",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"id": "0c581062-8967-4d93-b06e-62833222f930",
Expand Down Expand Up @@ -43,11 +49,244 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "5bda5f73",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products: list):\n",
" '''Initialize the inventory'''\n",
" inventory = {}\n",
" for prod in products:\n",
" prod_quant= input(f\"Quantity of product {prod}\")\n",
" inventory[prod] = int(prod_quant)\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "d5f7ea53",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'asd': 23, 'af': 54, 'kj': 65, 'oa': 1, 'kw': 54}"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"# products = [\"A\", \"B\", \"C\", \"D\", \"E\"]\n",
"from pydantic_core import ErrorTypeInfo\n",
"\n",
"\n",
"products_input = input(\"Enter list of products separated by space\")\n",
"try:\n",
" products = products_input.split()\n",
"except:\n",
" raise ErrorTypeInfo(\"Invalid Input\")\n",
"\n",
"begin_inventory = initialize_inventory(products)\n",
"begin_inventory"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "2a3e6da5",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" '''Enter Product Name'''\n",
" customer_orders = set()\n",
" cust_prod = input(f\"Enter first product: \")\n",
" add_another_prod = True\n",
" # more_prod = [\"y\",\"yes\", \"true\",1, \"Y\", \"Yes\",\"True\"]\n",
"\n",
" while add_another_prod == True:\n",
" available_prod = False\n",
" while available_prod == False : \n",
" if cust_prod in products:\n",
" customer_orders.add(cust_prod)\n",
" available_prod = True\n",
" else:\n",
" print(f\"The product {cust_prod} is not available\")\n",
" cust_prod = input(\"Next Product: \") \n",
"\n",
" add_prod_input = input(\"Do you want to add another product? (yes/no)\")\n",
" if add_prod_input.lower() == \"yes\" or add_prod_input.lower() == \"true\" or add_prod_input.lower() == \"y\" or add_prod_input == \"1\":\n",
" # if add_another_prod in more_prod: \n",
" cust_prod = input(\"Next Product: \")\n",
" else:\n",
" add_another_prod = False\n",
"\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "0c1339e5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The product afa is not available\n"
]
}
],
"source": [
"customer_orders = get_customer_orders()\n",
"# customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "e742ce26",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders: set, inventory: list):\n",
" '''Update inventory dict based on customer orders'''\n",
" new_inventory = {}\n",
" for key, val in inventory.items():\n",
" if key in customer_orders: \n",
" new_inventory[key] = val -1\n",
" elif inventory[key] == 0:\n",
" new_inventory[key] = 0\n",
" else:\n",
" new_inventory[key] = val\n",
" \n",
" return new_inventory"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "074d728f",
"metadata": {},
"outputs": [],
"source": [
"new_inventory = update_inventory(customer_orders=customer_orders, inventory=begin_inventory)\n",
"# new_inventory"
]
},
{
"cell_type": "code",
"execution_count": 68,
"id": "44e51891",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders: set, products: list):\n",
" '''calculate order statistics (total products ordered and percentage of unique products ordered'''\n",
" # # percent_unique_ = 0\n",
" # total_prod_ordered = len(customer_orders)\n",
" # total_prods = len(set(products))\n",
" # percent_unique_= round((total_prod_ordered / total_prods) * 100, 1)\n",
" # return [total_prod_ordered, percent_unique]\n",
" total_prod_ordered = len(customer_orders)\n",
" total_prods = len(set(products))\n",
" percent_unique = (total_prod_ordered / total_prods) * 100\n",
" return [total_prod_ordered, percent_unique]\n"
]
},
{
"cell_type": "code",
"execution_count": 69,
"id": "243778c7",
"metadata": {},
"outputs": [],
"source": [
"statistics = calculate_order_statistics (customer_orders=customer_orders, products = products )\n",
"# calculate_order_statistics (customer_orders=customer_orders, products = products )"
]
},
{
"cell_type": "code",
"execution_count": 70,
"id": "f7f76077",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" print(f\"Total prod ordered: {order_statistics[0]}\")\n",
" print(f\"Percentage of unique products ordered {order_statistics[1]}\")"
]
},
{
"cell_type": "code",
"execution_count": 72,
"id": "282504de",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total prod ordered: 2\n",
"Percentage of unique products ordered 40.0\n"
]
}
],
"source": [
"print_order_statistics(order_statistics = statistics)"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "31910327",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory: dict):\n",
" print(f\"Updated inventory: {inventory}\")"
]
},
{
"cell_type": "code",
"execution_count": 74,
"id": "3b919dca",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated inventory: {'asd': 22, 'af': 54, 'kj': 64, 'oa': 1, 'kw': 54}\n"
]
}
],
"source": [
"print_updated_inventory(new_inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "41498dc3",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +300,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down