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
338 changes: 334 additions & 4 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e",
"metadata": {},
"source": [
"# Lab | Functions"
"# Lab 03 | Functions\n",
"-------"
]
},
{
Expand Down Expand Up @@ -41,13 +42,342 @@
"- Utilize function parameters and return values to transfer data between functions.\n",
"- Test your functions individually to ensure they work correctly.\n",
"\n",
"\n"
"------\n"
]
},
{
"cell_type": "markdown",
"id": "d6bf0c1e",
"metadata": {},
"source": [
"# Solved Lab 03 | Functions\n",
"------"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9972bdb3",
"metadata": {},
"outputs": [],
"source": [
"#0. List of products from the previous lab\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "markdown",
"id": "91749d13",
"metadata": {},
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1ae5a04a",
"metadata": {},
"outputs": [],
"source": [
"#1. def Function : initialize_inventory(products)\n",
"# Initialize inventory dict using a loop and user input\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for item in products:\n",
" while True:\n",
" quantity = input(f\"Enter quantity for {item}: \")\n",
" if quantity.isdigit(): # Checks if the input contains only digits\n",
" inventory[item] = int(quantity) # key is item, value is quantity \n",
" print(f\"✅ Quantity = {inventory[item]} for {item}\")\n",
" break\n",
" else:\n",
" print(f\"⚠ Invalid input! Please enter a number only for {item}\")\n",
" return inventory"
]
},
{
"cell_type": "markdown",
"id": "ab98b5bd",
"metadata": {},
"source": [
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6c2aa017",
"metadata": {},
"outputs": [],
"source": [
"#2. def Function : get_customer_orders \n",
"def get_customer_orders():\n",
" products = {\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"}\n",
" customer_orders = set()\n",
"\n",
" product = \"\"\n",
" while product != \"purchase\":\n",
" product = input(\"Enter a product name to order (or type 'PURCHASE' to finish): \").strip().lower()\n",
"\n",
" # Empty input\n",
" if product == \"\":\n",
" print(\"⚠ You didn't type anything. Please enter a product name.\")\n",
" continue\n",
" \n",
" # Finish purchase\n",
" if product == \"purchase\":\n",
" break\n",
"\n",
" # Valid product\n",
" if product in products:\n",
" customer_orders.add(product)\n",
" print(f\"✅ '{product}' added to your order.\")\n",
"\n",
" # Invalid product\n",
" else:\n",
" print(f\"❌ '{product}' is not available.\")\n",
" print(f\"Available products: ,{products}\")\n",
"\n",
" print(\"\\n✅ Purchase complete!\")\n",
" print(\"Your purchase list:\", customer_orders)\n",
" return customer_orders"
]
},
{
"cell_type": "markdown",
"id": "415cf4c0",
"metadata": {},
"source": [
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "233c68ec",
"metadata": {},
"outputs": [],
"source": [
"#3. def Function : update_inventory(customer_orders, inventory)\n",
"def update_inventory(customer_orders:set, inventory:dict):\n",
" list_customer_orders = list(customer_orders) # store set as a list for processing\n",
" while True:\n",
" for item in list_customer_orders: # Use the list for iteration\n",
" # Update the inventory based on customer orders\n",
" if item in inventory and inventory[item] > 0: \n",
" inventory[item] -= 1 # Decrease the quantity by 1\n",
" # If the quantity reaches zero, remove the item from the inventory\n",
" elif item in inventory and inventory[item] == 0: \n",
" print(f\"{item} is out of stock and has been removed from the inventory.\")\n",
" # If the item is not found in the inventory, print a message\n",
" else: \n",
" print(f\"{item} is not available in the inventory.\")\n",
" return inventory"
]
},
{
"cell_type": "markdown",
"id": "65d44dad",
"metadata": {},
"source": [
"\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."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "96dc59c8",
"metadata": {},
"outputs": [],
"source": [
"#4. def Function : calculate_order_statistics(customer_orders, products)\n",
"\n",
"def calculate_order_statistics(customer_orders:set, products:list):\n",
"\n",
" # Total products ordered\n",
" total_products_ordered = len(customer_orders)\n",
"\n",
" # Unique products ordered that exist in the product list\n",
" unique_products_ordered = len(customer_orders.intersection(products))\n",
"\n",
" # Percentage calculation (avoid division by zero)\n",
" percentage_unique = (unique_products_ordered / len(products) * 100) if products else 0\n",
" \n",
" order_statistics = {\n",
" \"total_products_ordered\": total_products_ordered,\n",
" \"percentage_unique_products_ordered\": percentage_unique,\n",
" }\n",
" return total_products_ordered, percentage_unique\n"
]
},
{
"cell_type": "markdown",
"id": "77c72854",
"metadata": {},
"source": [
"\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."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c360caba",
"metadata": {},
"outputs": [],
"source": [
"#5. def Function : print_order_statistics(order_statistics)\n",
"# DICT\n",
"order_statistics = {}\n",
"order_statistics[\"total_products_ordered\"], order_statistics[\"percentage_unique_products_ordered\"] = calculate_order_statistics(get_customer_orders(), products)\n",
"\n",
"def print_order_statistics(order_statistics:dict):\n",
"\n",
" # Extract the dictionary if wrapped in a tuple\n",
" if isinstance(order_statistics, tuple) and len(order_statistics) > 0:\n",
" order_statistics = order_statistics[0] \n",
" # Print order statistics\n",
" for product, quantity in order_statistics.items():\n",
" print(f\"Product: {product}, Quantity Remaining: {quantity}\\n\") "
]
},
{
"cell_type": "markdown",
"id": "1d00fd4f",
"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."
]
},
{
"cell_type": "code",
"execution_count": 105,
"id": "15431b0d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated Inventory:\n",
"Product: t-shirt, Quantity: 10\n",
"Product: mug, Quantity: 20\n",
"Product: hat, Quantity: 30\n",
"Product: book, Quantity: 40\n",
"Product: keychain, Quantity: 50\n"
]
}
],
"source": [
"#6. def Function : print_updated_inventory(inventory)\n",
"def print_updated_inventory(inventory):\n",
" print(f\"Updated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"Product: {product}, Quantity: {quantity}\")\n",
"\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "markdown",
"id": "0b05f70a",
"metadata": {},
"source": [
"7. Call the functions in the appropriate sequence to execute the program and manage customer orders."
]
},
{
"cell_type": "code",
"execution_count": 103,
"id": "9c570942",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"List of products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"✅ Quantity = 10 for t-shirt\n",
"✅ Quantity = 20 for mug\n",
"✅ Quantity = 30 for hat\n",
"✅ Quantity = 40 for book\n",
"✅ Quantity = 60 for keychain\n",
"✅ 'keychain' added to your order.\n",
"❌ 'haty' is not available.\n",
"Available products: ,{'book', 't-shirt', 'keychain', 'mug', 'hat'}\n",
"✅ 'hat' added to your order.\n",
"✅ 'book' added to your order.\n",
"\n",
"✅ Purchase complete!\n",
"Your purchase list: {'keychain', 'hat', 'book'}\n",
"⚠ You didn't type anything. Please enter a product name.\n",
"⚠ You didn't type anything. Please enter a product name.\n",
"⚠ You didn't type anything. Please enter a product name.\n",
"⚠ You didn't type anything. Please enter a product name.\n",
"⚠ You didn't type anything. Please enter a product name.\n",
"⚠ You didn't type anything. Please enter a product name.\n",
"⚠ You didn't type anything. Please enter a product name.\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "Interrupted by user",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[103], line 16\u001b[0m\n\u001b[0;32m 12\u001b[0m get_customer_orders()\n\u001b[0;32m 15\u001b[0m \u001b[38;5;66;03m#3. call Function : update_inventory(customer_orders, inventory)\u001b[39;00m\n\u001b[1;32m---> 16\u001b[0m update_inventory(get_customer_orders(), initialize_inventory(products))\n\u001b[0;32m 18\u001b[0m \u001b[38;5;66;03m#4. call Function : calculate_order_statistics(customer_orders, products)\u001b[39;00m\n\u001b[0;32m 19\u001b[0m order_statistics \u001b[38;5;241m=\u001b[39m calculate_order_statistics(customer_orders\u001b[38;5;241m=\u001b[39mget_customer_orders(), products\u001b[38;5;241m=\u001b[39mproducts)\n",
"Cell \u001b[1;32mIn[98], line 8\u001b[0m, in \u001b[0;36mget_customer_orders\u001b[1;34m()\u001b[0m\n\u001b[0;32m 6\u001b[0m product \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 7\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m product \u001b[38;5;241m!=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpurchase\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m----> 8\u001b[0m product \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEnter a product name to order (or type \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mPURCHASE\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m to finish): \u001b[39m\u001b[38;5;124m\"\u001b[39m)\u001b[38;5;241m.\u001b[39mstrip()\u001b[38;5;241m.\u001b[39mlower()\n\u001b[0;32m 10\u001b[0m \u001b[38;5;66;03m# Empty input\u001b[39;00m\n\u001b[0;32m 11\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m product \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n",
"File \u001b[1;32mc:\\Users\\sboub\\anaconda3\\Lib\\site-packages\\ipykernel\\kernelbase.py:1282\u001b[0m, in \u001b[0;36mKernel.raw_input\u001b[1;34m(self, prompt)\u001b[0m\n\u001b[0;32m 1280\u001b[0m msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mraw_input was called, but this frontend does not support input requests.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 1281\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m StdinNotImplementedError(msg)\n\u001b[1;32m-> 1282\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_input_request(\n\u001b[0;32m 1283\u001b[0m \u001b[38;5;28mstr\u001b[39m(prompt),\n\u001b[0;32m 1284\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_parent_ident[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mshell\u001b[39m\u001b[38;5;124m\"\u001b[39m],\n\u001b[0;32m 1285\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mget_parent(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mshell\u001b[39m\u001b[38;5;124m\"\u001b[39m),\n\u001b[0;32m 1286\u001b[0m password\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[0;32m 1287\u001b[0m )\n",
"File \u001b[1;32mc:\\Users\\sboub\\anaconda3\\Lib\\site-packages\\ipykernel\\kernelbase.py:1325\u001b[0m, in \u001b[0;36mKernel._input_request\u001b[1;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[0;32m 1322\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m:\n\u001b[0;32m 1323\u001b[0m \u001b[38;5;66;03m# re-raise KeyboardInterrupt, to truncate traceback\u001b[39;00m\n\u001b[0;32m 1324\u001b[0m msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInterrupted by user\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m-> 1325\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m(msg) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m 1326\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[0;32m 1327\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mlog\u001b[38;5;241m.\u001b[39mwarning(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInvalid Message:\u001b[39m\u001b[38;5;124m\"\u001b[39m, exc_info\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n",
"\u001b[1;31mKeyboardInterrupt\u001b[0m: Interrupted by user"
]
}
],
"source": [
"#7. call all functions above in the appropriate sequence to execute the program and manage customer orders:\n",
"\n",
"# def List: products \n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"print('List of products:', products)\n",
"\n",
"# call Functions order:\n",
"#1. call Function : initialize_inventory(products)\n",
"initialize_inventory(products)\n",
"\n",
"#2. call Function : get_customer_orders()\n",
"get_customer_orders()\n",
"\n",
"\n",
"#3. call Function : update_inventory(customer_orders, inventory)\n",
"update_inventory(get_customer_orders(), initialize_inventory(products))\n",
"\n",
"#4. call Function : calculate_order_statistics(customer_orders, products)\n",
"order_statistics = calculate_order_statistics(customer_orders=get_customer_orders(), products=products)\n",
"\n",
"#5. call Function : print_order_statistics(order_statistics)\n",
"print_order_statistics(order_statistics)\n",
"\n",
"#6. call Function : print_updated_inventory(inventory)\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a378e516",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +391,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down