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
313 changes: 312 additions & 1 deletion lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,317 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3af9f4ba-d3e1-44e9-9820-1112ed2c26af",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 5,
"id": "fce46fe4-5f3f-4218-b8e0-e71e10fed0d5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter number 1: 23\n",
"Enter number 2: 34\n",
"Enter number 3: 12\n",
"Enter number 4: 34\n",
"Enter number 5: 11\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The inventory dictionary is : {'t-shirt': 23, 'mug': 34, 'hat': 12, 'book': 34, 'keychain': 11}\n"
]
}
],
"source": [
"inventory_dict = {}\n",
"def initialize_inventory(products):\n",
" \"\"\"\n",
" Initlaize the inventory dictionary\n",
"\n",
" Parameters:\n",
" Product list.\n",
" \"\"\"\n",
" for i in range(0, len(products)):\n",
" user_num = input(f\"Enter number {i+1}: \")\n",
" if user_num.isdigit():\n",
" inventory_dict[products[i]] = int(user_num)\n",
" else:\n",
" print(\"Enter the valid value\")\n",
" user_num = int(input(f\"Enter number {i+1}: \"))\n",
" inventory_dict[products[i]] = int(user_num)\n",
" \n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"print(products)\n",
"initialize_inventory(products)\n",
"print(\"The inventory dictionary is : \",inventory_dict)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "f9ca1518-6d01-4861-a9df-aafbd4622853",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the number of products to be ordered: 3\n"
]
}
],
"source": [
"count = int(input(\"Enter the number of products to be ordered: \"))\n",
"customer_orders = set()\n",
"\n",
"def get_customer_orders():\n",
" \"\"\"\n",
" Returns the customer orders set\n",
"\n",
" Parameters: Empty.\n",
" \"\"\"\n",
" for i in range(0,count):\n",
" product = input(f\"Enter the product {i+1}: \")\n",
" if product.isdigit():\n",
" product = print(f\"Enter the valid product {i+1}:\")\n",
" product = input(f\"Enter the product {i+1}: \")\n",
" customer_orders.add(product)\n",
" else:\n",
" customer_orders.add(product)\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "8f82a473-5606-487d-a5b1-f8f4fcec5658",
"metadata": {},
"outputs": [],
"source": [
"updated_inventory = {}\n",
"products = []\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" \"\"\"\n",
" Returns the updated inventory\n",
"\n",
" Parameters: customer_orders, inventory.\n",
" \"\"\"\n",
" for product in customer_orders:\n",
" while True:\n",
" quantity = input(f\"Enter the quantity for the product {product}: \")\n",
" if quantity.isdigit():\n",
" quantity = int(quantity)\n",
" break\n",
" print(\"Please enter a valid numeric quantity.\")\n",
" \n",
" products.append(quantity)\n",
"\n",
" # Update inventory if product exists\n",
" if product in inventory:\n",
" print(product)\n",
" inventory[product] = int(inventory[product]) - quantity\n",
" else:\n",
" print(f\"Warning: {product} not found in inventory!\")\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "b42807c3-0421-4243-aadd-5448a73eb720",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" \"\"\"\n",
" Calculate order statistics.\n",
"\n",
" Parameters: customer_orders, products.\n",
" \"\"\"\n",
" \n",
" total_order = sum(int(x) for x in products)\n",
" print(total_order)\n",
"\n",
" unique_percentage = (total_order / sum(inventory_dict.values())) * 100\n",
" return total_order, unique_percentage\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "6cf60fb6-1f1f-4c1a-8196-a648feea005e",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" \"\"\"\n",
" Calculate order statistics.\n",
"\n",
" Parameters: order statistics dictionary key as String and values as \n",
" percentage unique products value, total products ordered value.\n",
" \"\"\"\n",
" \n",
" percentage_ordered = order_statistics[\"percentage_unique_products\"]\n",
" print(\"Total products ordered: \",order_statistics[\"total_products_ordered\"])\n",
" print(f\"Total percentage of the unique products ordered: {percentage_ordered:.2f}\")\n",
"\n",
"#print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "d8bad1d3-a3fd-496c-bfc8-8f73a4f6e943",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated Inventory:\n",
"------------------\n"
]
}
],
"source": [
"def print_updated_inventory(inventory):\n",
" \"\"\"\n",
" Prints the updated inventory.\n",
"\n",
" Parameters:\n",
" inventory (dict): Dictionary with product names as keys and quantities as values.\n",
" \"\"\"\n",
" print(\"Updated Inventory:\")\n",
" print(\"------------------\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"print_updated_inventory(updated_inventory)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "0381a018-e661-40af-b6e0-5620a3c25f5a",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the product 1: hat\n",
"Enter the product 2: book\n",
"Enter the product 3: mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The product set is: {'hat', 'mug', 'book'}\n",
"{'t-shirt': 23, 'mug': 34, 'hat': 12, 'book': 34, 'keychain': 11}\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity for the product hat: 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"hat\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity for the product mug: 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"mug\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity for the product book: 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"book\n",
"7\n",
"Total products ordered: 7\n",
"Total percentage of the unique products ordered: 6.54\n",
"Updated Inventory:\n",
"------------------\n",
"t-shirt: 23\n",
"mug: 30\n",
"hat: 10\n",
"book: 33\n",
"keychain: 11\n"
]
}
],
"source": [
"\n",
"#Print the customers Order Set\n",
"print(\"The product set is: \",get_customer_orders())\n",
"\n",
"#get the updated inventory\n",
"print(inventory_dict)\n",
"updated_inventory = update_inventory(customer_orders, inventory_dict)\n",
"\n",
"#print the order statistics\n",
"total_order, unique_percentage = calculate_order_statistics(customer_orders, products)\n",
"order_statistics = {\"total_products_ordered\": total_order, \"percentage_unique_products\": unique_percentage}\n",
"print_order_statistics(order_statistics)\n",
"\n",
"#print the updated inventory \n",
"print_updated_inventory(updated_inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d3f64ac6-8be9-43e1-b538-099dcc14d52d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -61,7 +372,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.4"
}
},
"nbformat": 4,
Expand Down