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
308 changes: 305 additions & 3 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,315 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "bcb3eb06-c28c-4726-b9ba-9fa800b1de78",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e79e87ff-75c1-458c-841e-da2f0386efd1",
"metadata": {},
"outputs": [],
"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",
"\n",
"def initialize_inventory(products):\n",
" \n",
" inventory = {}\n",
" \n",
" for item in products:\n",
" \n",
" while True:\n",
" quantity = input(f\"How many units of {item} would you like to add to the inventory? \")\n",
" \n",
" if quantity.isdigit():\n",
" inventory[item] = int(quantity)\n",
" print(f\"Added {quantity} units of {item} to the inventory.\")\n",
" break\n",
" \n",
" else:\n",
" print(f\"Invalid input for '{item}'. Please enter a positive whole number.\")\n",
" \n",
" return inventory\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "4ae0e2a6-7e4e-43cc-af3a-aa3b0806eebf",
"metadata": {},
"outputs": [],
"source": [
"# 2 - Define a function named get_customer_orders that takes no parameters.\n",
"# 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",
"\n",
"def get_customer_orders():\n",
"\n",
" get_customer_orders = set()\n",
" print(\"\\nPRODUCTS LIST: \", \", \".join(products),\"\\n\")\n",
"\n",
" while True:\n",
" new_order = input(\"Do you want to input an order? (yes/no): \").strip().lower()\n",
" \n",
" if new_order == \"yes\":\n",
" order = input(\"From the products list, enter the name of a product that a customer wants to order: \").strip().lower()\n",
" \n",
" if order not in products:\n",
" print(f\"Error: '{order.upper()}' is not in the products list. Try again.\")\n",
" elif order in get_customer_orders:\n",
" print(f\"Error: You've already chosen '{order.upper()}'. Pick a different product.\")\n",
" else:\n",
" get_customer_orders.add(order)\n",
" print(f\"'{order.upper()}' has been added to your order. Total items selected: {len(get_customer_orders)}.\")\n",
" \n",
" elif new_order == \"no\":\n",
" break\n",
" \n",
" else:\n",
" print(\"Invalid input: must enter 'yes' or 'no'!\") \n",
" \n",
" return get_customer_orders\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "683524f5-deb2-4b41-851e-04ea47bab9ba",
"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",
"def update_inventory(customer_orders, inventory):\n",
"\n",
" for item in customer_orders:\n",
" if inventory[item] <= 0:\n",
" print(f\"Error: '{item.upper()}' is out of stock. Please choose another product.\")\n",
" else:\n",
" inventory[item] -= 1\n",
" print(f\"'{item.upper()}' quantity has been updated in inventory .\")\n",
"\n",
" return inventory\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3ce8ded8-4fd1-4cc5-b954-72044d75ab3c",
"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, and percentage of unique products ordered).\n",
"# The function should return these values.\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" total_unique_products_available = len(products)\n",
" \n",
" if total_unique_products_available > 0:\n",
" percentage_unique_products_ordered = (total_products_ordered / total_unique_products_available) * 100\n",
" else:\n",
" percentage_unique_products_ordered = 0\n",
" \n",
" return (total_products_ordered, percentage_unique_products_ordered)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "f3f0f8c9-6ae0-455a-8745-87885fe3cfaa",
"metadata": {},
"outputs": [],
"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(order_statistics):\n",
" total_ordered, percentage_ordered = order_statistics\n",
" print(\"\\nORDER STATISTICS:\")\n",
" print(f\"Total items selected: {total_ordered}\")\n",
" print(f\"Percentage of unique products ordered: {percentage_ordered:.2f}%\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "f4f35db3-1638-460a-b83d-9caf42b266ec",
"metadata": {},
"outputs": [],
"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",
"\n",
"def print_updated_inventory(inventory):\n",
" print(\"\\nUpdated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product.title()}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "8a27d8d7-f1e6-495c-a679-a3959e1e0e69",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"How many units of t-shirt would you like to add to the inventory? 10\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Added 10 units of t-shirt to the inventory.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"How many units of mug would you like to add to the inventory? 10\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Added 10 units of mug to the inventory.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"How many units of hat would you like to add to the inventory? 10\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Added 10 units of hat to the inventory.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"How many units of book would you like to add to the inventory? 10\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Added 10 units of book to the inventory.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"How many units of keychain would you like to add to the inventory? 10\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Added 10 units of keychain to the inventory.\n",
"\n",
"PRODUCTS LIST: t-shirt, mug, hat, book, keychain \n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to input an order? (yes/no): yes\n",
"From the products list, enter the name of a product that a customer wants to order: mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"'MUG' has been added to your order. Total items selected: 1.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to input an order? (yes/no): no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"'MUG' quantity has been updated in inventory .\n",
"\n",
"Updated Inventory:\n",
"T-Shirt: 10\n",
"Mug: 9\n",
"Hat: 10\n",
"Book: 10\n",
"Keychain: 10\n",
"\n",
"ORDER STATISTICS:\n",
"Total items selected: 1\n",
"Percentage of unique products ordered: 20.00%\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"customer_orders = get_customer_orders()\n",
"\n",
"update_inventory(customer_orders, inventory)\n",
"\n",
"print_updated_inventory(inventory)\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"\n",
"print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "17ba2cf9-2d44-4122-967b-e7d4a55e0cdc",
"metadata": {},
"outputs": [],
"source": []
}
],
"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 +363,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down