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
124 changes: 119 additions & 5 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"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",
"#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",
"#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",
Expand All @@ -39,15 +39,129 @@
"\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",
"- Test your functions individually to ensure they work correctly.#\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7078b47b",
"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: 10\n",
"mug: 10\n",
"hat: 9\n",
"book: 9\n",
"keychain: 10\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" while True:\n",
" quantity = int(input(f\"Enter quantity for {product}: \"))\n",
" if quantity > 0:\n",
" inventory[product] = quantity\n",
" break\n",
" else:\n",
" print(\"Quantity must be a positive integer. Please try again.\")\n",
" return inventory\n",
"\n",
"inventory = initialize_inventory(products)\n",
"print(\"Inventario: \", inventory)\n",
" \n",
"\n",
"\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" while True:\n",
" order = input(\"Enter a product to order (t-shirt, mug, hat, book, keychain): \").strip().lower()\n",
" if order in products:\n",
" customer_orders.add(order)\n",
" while True: \n",
" o = input(\"Do you want to order another product? (yes/no): \").strip().lower() \n",
" if o == \"no\":\n",
" print(f\"Your order is completed. The order contains {customer_orders}.\")\n",
" return customer_orders \n",
" elif o == \"yes\":\n",
" break \n",
" else:\n",
" print(\"Invalid input. Please enter 'yes' or 'no'.\") \n",
" else:\n",
" print(\"Sorry. That prodcut is not aviable.\") \n",
" \n",
"costumers_orders = get_customer_orders()\n",
"print(\"Costumers orders: \", costumers_orders)\n",
"\n",
"\n",
"\n",
"def update_inventory(costumers_orders, inventory):\n",
" for order in costumers_orders:\n",
" if order in inventory and inventory[order] > 0:\n",
" inventory[order] -= 1\n",
" else:\n",
" print(f\"Sorry, {order} is out of stock.\")\n",
" return inventory\n",
"\n",
"inventory = update_inventory(costumers_orders, inventory)\n",
"print(\"Updated inventory: \", inventory)\n",
"\n",
"\n",
"def calculate_order_statistics(costumers_orders, products):\n",
" total_products_ordered = len(costumers_orders)\n",
" percentage_ordered = (total_products_ordered / len(products)) * 100\n",
" return total_products_ordered, percentage_ordered\n",
"\n",
"total_products_ordered, percentage_ordered = calculate_order_statistics(costumers_orders, products)\n",
"print(f\"Total products ordered: {total_products_ordered}\")\n",
"\n",
"def print_order_stadistics(order_stadistics):\n",
" total_products_ordered, percentage_ordered = order_stadistics\n",
" print(f\"Total products ordered: {total_products_ordered}\")\n",
" print(f\"Percentage of products ordered: {percentage_ordered:.2f}%\")\n",
"\n",
"print_order_stadistics((total_products_ordered, percentage_ordered))\n",
" \n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9740e928",
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +175,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.9"
}
},
"nbformat": 4,
Expand Down