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
282 changes: 279 additions & 3 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,289 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 127,
"id": "e09cf4d5-7c1c-4e55-9e5a-a55e858d10a3",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the quantity of 't-shirt' in the inventory: 10\n",
"Please enter the quantity of 'mug' in the inventory: 10\n",
"Please enter the quantity of 'hat' in the inventory: 10\n",
"Please enter the quantity of 'book' in the inventory: 10\n",
"Please enter the quantity of 'keychain' in the inventory: 10\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n"
]
}
],
"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 a loop and user input\n",
"#products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products: list) -> dict:\n",
" \"\"\"\n",
" This function implements the code for initializing the inventory dictionary using a loop and user \n",
" \"\"\"\n",
" init_invent = {}\n",
" for product in products:\n",
" init_invent[product] = int(input(f\"Please enter the quantity of '{product}' in the inventory: \"))\n",
"\n",
" return init_invent\n",
"\n",
"inventory = initialize_inventory(products)\n",
"print(inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": 128,
"id": "707b0672-9a26-427d-9a4c-db30d6e380ea",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter your product: HAT\n",
"Do you want to order an other product? answer Yes/No: YES\n",
"Please enter your product: BOOK\n",
"Do you want to order an other product? answer Yes/No: YES\n",
"Please enter your product: T-SHIRT\n",
"Do you want to order an other product? answer Yes/No: YES\n",
"Please enter your product: MUG\n",
"Do you want to order an other product? answer Yes/No: N\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer's order list: {'mug', 'book', 't-shirt', 'hat'}\n"
]
}
],
"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.\n",
"#The function should return the customer_orders set.\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def get_customer_orders():\n",
" \"\"\"\n",
" This implements the code for prompting the user to enter the product names using a loop.\n",
" The function should return the customer_orders set\n",
" \"\"\"\n",
" customer_orders = set()\n",
" continue_order = \"yes\"\n",
" while continue_order == \"yes\":\n",
" product = input (\"Please enter your product: \").lower()\n",
" \n",
" for i in range(len(products)):\n",
" if product == products[i]:\n",
" customer_orders.add(product)\n",
" \n",
" continue_order = input(\"Do you want to order an other product? answer Yes/No: \").lower()\n",
" \n",
" return customer_orders\n",
"customer_orders = get_customer_orders()\n",
"\n",
"\n",
"print(f\"Customer's order list: {customer_orders}\")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 129,
"id": "7ccb1914-3e1b-4b9c-9d4b-a6ff003e9b7b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"initial inventory is: {'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n",
"Products order: {'mug', 'book', 't-shirt', 'hat'}\n",
"The updated inventory is: {'t-shirt': 9, 'mug': 9, 'hat': 9, 'book': 9, 'keychain': 10}\n"
]
}
],
"source": [
"# 3. Define a function named update_inventory that takes customer_orders\n",
"# and inventory as parameters. Inside the function, implement the code \n",
"# for updating the inventory dictionary based on the customer orders.\n",
"print(\"initial inventory is:\", inventory)\n",
"print(\"Products order: \", customer_orders)\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" \n",
" for product in customer_orders:\n",
" inventory[product] -= 1\n",
" return inventory\n",
" \n",
"inventory_update = update_inventory(customer_orders, inventory)\n",
"print(f\"The updated inventory is: {inventory_update}\")\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 130,
"id": "01fc9645-a634-470c-b66c-0f177c0541b6",
"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, \n",
"#and percentage of unique products ordered). \n",
"#The function should return these values.\n",
"def calculate_order_statistics (customer_orders, products):\n",
" total_ord_prod = len(customer_orders)\n",
" item_perc = {}\n",
" for item in customer_orders:\n",
" item_perc[item] = 1/inventory[item] *100\n",
" return total_ord_prod, item_perc #output the total ordered products, and pert per ordered item as dict"
]
},
{
"cell_type": "code",
"execution_count": 131,
"id": "7dcf03ea-ba92-4f30-95ce-1ea1b5d808df",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The total of ordered products is 4\n",
"The percentage of product'mug' with respect to the total products is: 11.11 %\n",
"The percentage of product'book' with respect to the total products is: 11.11 %\n",
"The percentage of product't-shirt' with respect to the total products is: 11.11 %\n",
"The percentage of product'hat' with respect to the total products is: 11.11 %\n"
]
}
],
"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(customer_orders, products):\n",
" stat1, stat2 = calculate_order_statistics (customer_orders, products)\n",
" print(\"The total of ordered products is\", stat1)\n",
" for key, value in stat2.items():\n",
" print(f\"The percentage of product'{key}' with respect to the total products is: {value: .2f} %\")\n",
"\n",
"print_order_statistics(customer_orders, products)\n"
]
},
{
"cell_type": "code",
"execution_count": 132,
"id": "39de443f-f029-4a27-9d30-5ecbb321b19a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 8, 'mug': 8, 'hat': 8, 'book': 8, 'keychain': 10}\n"
]
}
],
"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",
"def print_updated_inventory(inventory):\n",
" upd_inventory = update_inventory(customer_orders, inventory)\n",
" print(upd_inventory)\n",
" \n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 133,
"id": "bd8ee92b-427b-4b32-9d1f-f4f91a0d5420",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the quantity of 't-shirt' in the inventory: 10\n",
"Please enter the quantity of 'mug' in the inventory: 10\n",
"Please enter the quantity of 'hat' in the inventory: 10\n",
"Please enter the quantity of 'book' in the inventory: 10\n",
"Please enter the quantity of 'keychain' in the inventory: 10\n",
"Please enter your product: 10\n",
"Do you want to order an other product? answer Yes/No: YES\n",
"Please enter your product: HAT\n",
"Do you want to order an other product? answer Yes/No: YES\n",
"Please enter your product: BOOK\n",
"Do you want to order an other product? answer Yes/No: YES\n",
"Please enter your product: MUG\n",
"Do you want to order an other product? answer Yes/No: YES\n",
"Please enter your product: T-SHIRT\n",
"Do you want to order an other product? answer Yes/No: NO\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The total of ordered products is 4\n",
"The percentage of product'mug' with respect to the total products is: 12.50 %\n",
"The percentage of product'book' with respect to the total products is: 12.50 %\n",
"The percentage of product't-shirt' with respect to the total products is: 12.50 %\n",
"The percentage of product'hat' with respect to the total products is: 12.50 %\n",
"{'t-shirt': 6, 'mug': 6, 'hat': 6, 'book': 6, 'keychain': 10}\n"
]
}
],
"source": [
"# 7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"#1. Initialize inventory: \n",
"initialize_inventory(products)\n",
" \n",
"#2. collect customer products: input directly from customer\n",
"get_customer_orders()\n",
" \n",
"#3. calculate statistics of ordered products\n",
"calculate_order_statistics(customer_orders, products)\n",
"\n",
"#4. Print order stats\n",
"print_order_statistics(customer_orders, products)\n",
"\n",
"#5. Update inventory:\n",
"update_inventory(customer_orders, inventory)\n",
"\n",
"#6. Print updated inventory\n",
"print_updated_inventory(inventory)\n",
"\n",
"\n"
]
}
],
"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 +337,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down