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
344 changes: 342 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,351 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 69,
"id": "a6049bc9",
"metadata": {},
"outputs": [],
"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",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"] #initial product list\n",
"inventory={}#empty inventory dictionary\n",
"\n",
"def initialize_inventory(products):\n",
" #initialize the inventory dictionary using a loop and user input\n",
" for product in products: #iterate over the product list \n",
" inventory[f\"{product}\"] = int(input(f'Enter the quantity of \"{product}\" in the inventory: '))\n",
" return inventory\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 98,
"id": "a77e30a2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}"
]
},
"execution_count": 98,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 99,
"id": "75366df4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inventory"
]
},
{
"cell_type": "code",
"execution_count": 101,
"id": "f4e1f4ad",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'book', 'hat', 'mug'}"
]
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 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 should return the `customer_orders` set.\n",
"\n",
"customer_order=set()\n",
"def get_customer_orders():\n",
" customer_order.add(input(\"enter a product you want to order between t-shirt, mug, hat, book, keychain \"))\n",
" while input(\"do you want to order another product? (yes/no)\") == \"yes\":\n",
" customer_order.add(input(\"enter the name of a product you want to order between t-shirt, mug, hat, book, keychain\"))\n",
" else:\n",
"\n",
" return customer_order\n",
" \n",
"get_customer_orders()\n",
"\n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 103,
"id": "a431c681",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}"
]
},
"execution_count": 103,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e7f2f56a",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "ffe102ee",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "38a28361",
"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",
" \n",
"def update_inventory(customer_order, inventory):\n",
" for product in customer_order:\n",
" if product in inventory:\n",
" inventory[product] -= 1\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 110,
"id": "c02c1c18",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 10, 'mug': 8, 'hat': 8, 'book': 8, 'keychain': 10}"
]
},
"execution_count": 110,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"update_inventory(customer_order, inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": 111,
"id": "ef86aa6a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'book', 'hat', 'mug'}"
]
},
"execution_count": 111,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"customer_order"
]
},
{
"cell_type": "code",
"execution_count": 184,
"id": "4b0b287d",
"metadata": {},
"outputs": [],
"source": [
"# 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 percentage of unique products ordered). \n",
"# The function should return these values.\n",
"\n",
"def calculate_order_statistics(customer_order, products):\n",
" totalorder=len(customer_order)\n",
" uniqueproducts=totalorder/len(products)*100\n",
" order_statistics=(totalorder, uniqueproducts)\n",
" return order_statistics \n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 185,
"id": "30b8eb58",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(3, 60.0)\n"
]
}
],
"source": [
"calculate_order_statistics(customer_order, products)\n",
"order_statistics = calculate_order_statistics(customer_order, products) \n",
"print(order_statistics)\n"
]
},
{
"cell_type": "code",
"execution_count": 186,
"id": "b4d41785",
"metadata": {},
"outputs": [],
"source": [
"#Define a function named `print_order_statistics` that takes `order_statistics` \n",
"# as a parameter. Inside the function, implement the code for printing the order statistics.\n",
"def print_order_statistics(order_statistics):\n",
" print(f\"Total products ordered: {order_statistics[0]}\")\n",
" print(f\"Percentage of unique products ordered: {order_statistics[1]:.2f}%\")"
]
},
{
"cell_type": "code",
"execution_count": 187,
"id": "8dab6fa2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total products ordered: 3\n",
"Percentage of unique products ordered: 60.00%\n"
]
}
],
"source": [
"print_order_statistics(order_statistics)\n"
]
},
{
"cell_type": "code",
"execution_count": 188,
"id": "c6fb2921",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-shirt: 10\n",
"mug: 8\n",
"hat: 8\n",
"book: 8\n",
"keychain: 10\n"
]
}
],
"source": [
"#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",
"def print_updated_inventory(inventory):\n",
" for products,quantities in inventory.items():\n",
" print(f\"{products}: {quantities}\")\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 190,
"id": "99737842",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n",
"{'book', 'mug', 't-shirt', 'hat'}\n",
"(4, 80.0)\n",
"Total products ordered: 4\n",
"Percentage of unique products ordered: 80.00%\n",
"t-shirt: 9\n",
"mug: 9\n",
"hat: 9\n",
"book: 9\n",
"keychain: 10\n"
]
}
],
"source": [
"#Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"\n",
"initialize_inventory(products)\n",
"print(inventory)\n",
"get_customer_orders()\n",
"print(customer_order)\n",
"update_inventory(customer_order, inventory)\n",
"print\n",
"calculate_order_statistics(customer_order, products) \n",
"print(order_statistics)\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory(inventory)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c7aed371",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +401,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down