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
152 changes: 150 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,159 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 85,
"id": "18c7214a",
"metadata": {},
"outputs": [],
"source": [
"# Define a function named `initialize_inventory` that takes `products` as a parameter. \n",
"def initialize_inventory(products):\n",
" #Inside the function, implement the code for initializing the inventory dictionary using a loop and user input.\n",
" inventory = {product: int(input(f\"Please input the amount of the product {product}\")) for product in products}\n",
" #return dictionary\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c8669383",
"metadata": {},
"outputs": [],
"source": [
"#Define a function named `get_customer_orders` that takes no parameters. \n",
"def get_customer_orders():\n",
"#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",
" customer_orders = set()\n",
" \n",
" while True:\n",
" order = input(\"please enter the product you would like to order:\") \n",
" #add to customer_order\n",
" customer_orders.add(order)\n",
" #ask for more\n",
" next_order = input(\"Would you like to order something else? Please enter 'yes' or 'no':\").strip().lower() \n",
" if next_order == 'no':\n",
" break\n",
" #return set\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c7a40696",
"metadata": {},
"outputs": [],
"source": [
"#Define a function named `update_inventory` that takes `customer_orders` and `inventory` as parameters. \n",
"def update_inventory(customer_orders, inventory):\n",
"# Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n",
" return = {product: (quantity -1 if product in customer_orders else quantity) for product, quantity in inventory.items()}\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 97,
"id": "64a9b017",
"metadata": {},
"outputs": [],
"source": [
"#Define a function named `calculate_order_statistics` that takes `customer_orders` and `products` as parameters. \n",
"def calculate_order_statistics(customer_orders, products):\n",
"# Inside the function, implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). \n",
" total_products_ordered = len(customer_orders)\n",
" percentage_unique_products = (total_products_ordered / len(products)) *100\n",
"# The function should return these values.\n",
" return total_products_ordered, percentage_unique_products"
]
},
{
"cell_type": "code",
"execution_count": 99,
"id": "034aafc1",
"metadata": {},
"outputs": [],
"source": [
"#Define a function named `print_order_statistics` that takes `order_statistics` as a parameter. \n",
"def print_order_statistics(order_statistics):\n",
" #define order_statistics\n",
" total_products_ordered, percentage_unique_products = order_statistics\n",
"\n",
" #Inside the function, implement the code for printing the order statistics.\n",
" print(f\"Total products ordered: {total_products_ordered}\\n\"\n",
" f\"Percentage of unique products ordered: {percentage_unique_products: .2f}%\")"
]
},
{
"cell_type": "code",
"execution_count": 104,
"id": "fd622d02",
"metadata": {},
"outputs": [],
"source": [
"#Define a function named `print_updated_inventory` that takes `inventory` as a parameter. \n",
"def print_updated_inventory(inventory):\n",
"#Inside the function, implement the code for printing the updated inventory.\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": 105,
"id": "36fd9f60",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Order Statistics:\n",
"Total products ordered: 2\n",
"Percentage of unique products ordered: 40.00%\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 98\n",
"mug: 64\n",
"hat: 31\n",
"book: 78\n",
"keychain: 45\n"
]
}
],
"source": [
"#Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"# define products\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"updated_inventory = update_inventory(customer_orders, inventory)\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"#print\n",
"print(\"\\nOrder Statistics:\")\n",
"print_order_statistics(order_statistics)\n",
"print(\"\\nUpdated Inventory:\")\n",
"print_updated_inventory(updated_inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "26531521",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +209,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.2"
}
},
"nbformat": 4,
Expand Down