Skip to content
Open
Show file tree
Hide file tree
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
171 changes: 171 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e",
"metadata": {},
"source": [
"# Lab | Functions"
]
},
{
"cell_type": "markdown",
"id": "0c581062-8967-4d93-b06e-62833222f930",
"metadata": {
"tags": []
},
"source": [
"## Exercise: Managing Customer Orders with Functions\n",
"\n",
"In the previous exercise, you improved the code for managing customer orders by using loops and flow control. Now, let's take it a step further and refactor the code by introducing functions.\n",
"\n",
"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",
"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",
"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",
"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",
"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"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "e5b19036",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(f\"Enter the quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
" return inventory "
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "3cb2681f",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders(inventory):\n",
" customer_orders = {}\n",
" while True:\n",
" product = input(\"Enter the product you want to order: \").strip().lower()\n",
" \n",
" if product in inventory:\n",
" quantity = int(input(f\"Enter the quantity for {product}: \"))\n",
" \n",
" if quantity > inventory[product]:\n",
" print(f\"Sorry, we only have {inventory[product]} of {product} available.\")\n",
" continue\n",
" \n",
" customer_orders[product] = customer_orders.get(product, 0) + quantity\n",
" else:\n",
" print(f\"Sorry, we don't have {product} in our inventory.\")\n",
" continue\n",
"\n",
" customer_orders[product] = customer_orders.get(product, 0) + quantity\n",
" \n",
" another = input(\"Do you want to order another product? (yes/no): \").strip().lower()\n",
" if another != 'yes':\n",
" break\n",
" \n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "741bb2ac",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(inventory, customer_orders):\n",
" for product, quantity in customer_orders.items():\n",
" inventory[product] -= quantity\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "9e56589a",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(orders, products):\n",
" total_items = sum(orders.values())\n",
" unique_products = len(orders)\n",
" percent_unique = (unique_products / len(products)) * 100\n",
" return total_items, percent_unique"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "6c4ce05c",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" total, percentage = order_statistics\n",
" print(\"Order Statistics:\")\n",
" print (f\"Total items ordered: {total}\")\n",
" print (\"Percentage of unique products ordered: {percentage:.2f}%\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "c94b14f7",
"metadata": {},
"outputs": [],
"source": [
"def print_updater_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
158 changes: 157 additions & 1 deletion lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,162 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9dcab7fd-cda4-4ddc-b957-fb646bf1e110",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Set up your inventory\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the initial quantity for {product}: 1\n",
"Enter the initial quantity for {product}: 1\n",
"Enter the initial quantity for {product}: 1\n",
"Enter the initial quantity for {product}: 1\n",
"Enter the initial quantity for {product}: 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Taking customer orders...\n",
"Customer Order Section\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"\n",
"Enter the product you want to order (t-shirt, mug, hat, book, keychain): banan\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sorry, product is not available in our store.\n"
]
}
],
"source": [
"products = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"\n",
"def initialize_inventory(products):\n",
"\n",
" inventory = {}\n",
" for product in products:\n",
" while True:\n",
" try:\n",
" quantity = int(input(\"Enter the initial quantity for {product}: \"))\n",
" if quantity < 0:\n",
" print(\"Please enter a non-negative number.\")\n",
" continue\n",
" inventory[product] = quantity\n",
" break\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a number.\")\n",
" return inventory\n",
"\n",
"def get_customer_orders():\n",
"\n",
" customer_orders = {}\n",
" print(\"Customer Order Section\")\n",
" while True:\n",
" product = input(\"\\nEnter the product you want to order (t-shirt, mug, hat, book, keychain): \").strip().lower()\n",
" \n",
" if product not in products:\n",
" print(\"Sorry, product is not available in our store.\")\n",
" else:\n",
" while True:\n",
" try:\n",
" quantity = int(input(\"How many {product}s would you like to order? \"))\n",
" if quantity <= 0:\n",
" print(\"Please enter a positive number.\")\n",
" continue\n",
" customer_orders[product] = customer_orders.get(product, 0) + quantity\n",
" break\n",
" except ValueError:\n",
" print(\"Please enter a valid number.\")\n",
"\n",
" another = input(\"Would you like to order another product? (yes/no): \").strip().lower()\n",
" if another != 'yes':\n",
" break\n",
" return customer_orders\n",
"\n",
"def update_inventory(inventory, customer_orders):\n",
"\n",
" for product, quantity in customer_orders.items():\n",
" if product in inventory:\n",
" inventory[product] -= quantity\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
"\n",
" total_ordered = sum(customer_orders.values())\n",
" unique_ordered_count = len(customer_orders)\n",
" percentage_unique = (unique_ordered_count / len(products)) * 100\n",
" return total_ordered, percentage_unique\n",
"\n",
"def print_order_statistics(order_statistics):\n",
"\n",
" total_ordered, percentage_unique = order_statistics\n",
" print(\"Order Statistics:\")\n",
" print(f\" Total items ordered: {total_ordered}\")\n",
" print(f\" Percentage of unique products ordered: {percentage_unique:.1f}%\")\n",
"\n",
"def print_updated_inventory(inventory):\n",
"\n",
" print(\"Updated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" status = \"Low stock\" if quantity == 0 else str(quantity)\n",
" print(f\" {product}: {status}\")\n",
"\n",
"if __name__ == \"__main__\":\n",
" \n",
" \n",
" print(\"Set up your inventory\")\n",
" inventory = initialize_inventory(products)\n",
" \n",
" \n",
" print(\"Taking customer orders...\")\n",
" customer_orders = get_customer_orders()\n",
" \n",
" if not customer_orders:\n",
" print(\"No orders were placed.\")\n",
" else:\n",
" \n",
" print(\"Calculating statistics...\")\n",
" stats = calculate_order_statistics(customer_orders, products)\n",
" print_order_statistics(stats)\n",
" \n",
" \n",
" print(\"Updating inventory...\")\n",
" update_inventory(inventory, customer_orders)\n",
" \n",
" \n",
" print_updated_inventory(inventory)\n",
" \n",
" print(\"Program finished.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "37311dff-947b-4667-8b55-212022a6026b",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -61,7 +217,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down