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
317 changes: 308 additions & 9 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,331 @@
"Follow the steps below to complete the exercise:\n",
"\n",
"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",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "dd00d8c9",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" inventory[product] = int(input(f\"How many {product}s do you have in storage?\"))\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "5ba3b18f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 45, 'mug': 98, 'hat': 34, 'book': 98, 'keychain': 50}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inventory = initialize_inventory([\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"])\n",
"inventory"
]
},
{
"cell_type": "markdown",
"id": "ebbc8c39",
"metadata": {},
"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. The function should return the `customer_orders` set.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "451c5596",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" products = []\n",
" still_buying = True\n",
" while still_buying:\n",
" product = input(\"What products would you like to buy?\")\n",
" customer_answer = input(\"Do you wish to buy another product? (Y/N)\")\n",
"\n",
" # if customer_answer not in [\"Y\", \"N\"]:\n",
" # customer_answer = input(\"Do you wish to buy another product? (Y/N)\")\n",
" if customer_answer == \"N\":\n",
" still_buying = False\n",
"\n",
" products.append(product)\n",
" return products"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "be3ee4c8",
"metadata": {},
"outputs": [],
"source": [
"customer_orders = get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "84812d28",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['iguana', 'mug', 'book', 't-shirt', '', 'non']"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"customer_orders"
]
},
{
"cell_type": "markdown",
"id": "dc7f1fde",
"metadata": {},
"source": [
"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",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "c365da88",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" # update the inventory dictionary based on customer orders\n",
" # first check the products that the customer bought one at a time\n",
" for product in customer_orders:\n",
" # see if I have the products in the inventory\n",
" if product in inventory:\n",
" # if I do have the product in inventory, remove 1 item of that product from the inventory\n",
" inventory[product] -= 1\n",
" # if I don't have the product in inventory, let the customer know it's not there\n",
" else:\n",
" print(f\"{product} is not in inventory!\")\n",
" # break\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "87d4432a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"iguana is not in inventory!\n",
" is not in inventory!\n",
"non is not in inventory!\n"
]
},
{
"data": {
"text/plain": [
"{'t-shirt': 44, 'mug': 97, 'hat': 34, 'book': 97, 'keychain': 50}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"update_inventory(customer_orders, inventory)"
]
},
{
"cell_type": "markdown",
"id": "e1e0f6d0",
"metadata": {},
"source": [
"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"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "18de36f4",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" unique_products_ordered = len(set(customer_orders))\n",
" if len(products) > 0:\n",
" percentage_unique_products_ordered = (unique_products_ordered / len(products)) * 100\n",
" else:\n",
" percentage_unique_products_ordered = 0\n",
" return total_products_ordered, percentage_unique_products_ordered\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "84b82824",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(6, 120.0)"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"calculate_order_statistics(customer_orders, [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"])"
]
},
{
"cell_type": "markdown",
"id": "e6c4506d",
"metadata": {},
"source": [
"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"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "d28d2b3f",
"metadata": {},
"outputs": [],
"source": [
"order_statistics = calculate_order_statistics(customer_orders, [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"])"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "37306f19",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" print(\"Total products ordered:\", order_statistics[0])\n",
" print(\"Percentage of unique products ordered:\", order_statistics[1], \"%\")\n"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "8969a017",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total products ordered: 6\n",
"Percentage of unique products ordered: 120.0 %\n"
]
}
],
"source": [
"print_order_statistics(order_statistics)"
]
},
{
"cell_type": "markdown",
"id": "442fac5b",
"metadata": {},
"source": [
"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",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "90cd8df1",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" for product, quantity in inventory.items():\n",
" print(product + \":\", quantity)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "b228e0eb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-shirt: 44\n",
"mug: 97\n",
"hat: 34\n",
"book: 97\n",
"keychain: 50\n"
]
}
],
"source": [
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "markdown",
"id": "c55c3a7c",
"metadata": {},
"source": [
"7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"\n",
"Hints for functions:\n",
"\n",
"- Consider the input parameters required for each function and their return values.\n",
"- Utilize function parameters and return values to transfer data between functions.\n",
"- Test your functions individually to ensure they work correctly.\n",
"\n",
"\n"
"- Test your functions individually to ensure they work correctly."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +360,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down