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
377 changes: 377 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,377 @@
{
"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": "markdown",
"id": "c534f7f9-e1ef-45f5-8cfe-ae87a3b54685",
"metadata": {},
"source": [
"1. Define a function named initialize_inventory"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "9e5c4ef8-92eb-4963-97c1-ff16ac61a4de",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products):\n",
" inventory_dictionary = {}\n",
" \n",
" for product in products: # estou a percorrer a lista de produtos\n",
" quantity = int(input(f\"Enter the quantity for {product}: \"))\n",
" inventory_dictionary[product] = quantity\n",
" \n",
" return inventory_dictionary"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "5bbeb9f5-8dc4-44ab-9727-1f63fb33c05b",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity for t-shirt: 45\n",
"Enter the quantity for mug: 23\n",
"Enter the quantity for hat: 9\n",
"Enter the quantity for book: 21\n",
"Enter the quantity for keychain: 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 45, 'mug': 23, 'hat': 9, 'book': 21, 'keychain': 4}\n"
]
}
],
"source": [
"inventory_dictionary = initialize_inventory(products)\n",
"print(inventory_dictionary)"
]
},
{
"cell_type": "markdown",
"id": "ce9857b7-0aa2-4805-b04d-2a81867f49c1",
"metadata": {},
"source": [
"2. Define a function named get_customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "e3feaac9-6d0c-4db1-abf7-79efdf6cf5a4",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "65445c73-7c54-426b-bc7f-78dc56b23131",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" customer_orders = set()\n",
"\n",
" asking_order = \"yes\"\n",
"\n",
" while asking_order == \"yes\":\n",
" asking_order = (input(\"Enter your order: \"))\n",
" customer_orders.add(asking_order)\n",
"\n",
" asking_order = input(\"Do you want to enter place another order? (yes/no): \").lower()\n",
" \n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "6264323b-d17a-4bb7-a467-c5b2e20167e0",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter your order: keychain\n",
"Do you want to enter place another order? (yes/no): yes\n",
"Enter your order: book\n",
"Do you want to enter place another order? (yes/no): no\n"
]
},
{
"data": {
"text/plain": [
"{'book', 'keychain'}"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_customer_orders()"
]
},
{
"cell_type": "markdown",
"id": "29ea79af-1543-46fe-8ff4-ffb29a746da9",
"metadata": {},
"source": [
"3. Define a function named update_inventory that takes customer_orders and inventory as parameters"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "752e79c4-d2e5-48b7-b417-80f89ef3cfad",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
"\n",
" for order in customer_orders:\n",
"\n",
" if order not in inventory: \n",
" print(f\"The product doesn't exist\")\n",
"\n",
" else:\n",
" inventory[order] = inventory[order] - 1\n",
" \n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "a616b660-e66f-43cd-8411-3c73574d6a7e",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity for t-shirt: 45\n",
"Enter the quantity for mug: 67\n",
"Enter the quantity for hat: 32\n",
"Enter the quantity for book: 45\n",
"Enter the quantity for keychain: 90\n",
"Enter your order: keychain\n",
"Do you want to enter place another order? (yes/no): no\n"
]
},
{
"data": {
"text/plain": [
"{'t-shirt': 45, 'mug': 67, 'hat': 32, 'book': 45, 'keychain': 89}"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"update_inventory(customer_orders, inventory)"
]
},
{
"cell_type": "markdown",
"id": "75413ff4-21d7-4e4f-8588-c4b2ff683f4a",
"metadata": {},
"source": [
"4. Define a function named calculate_order_statistics that takes customer_orders and products as parameters."
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "b0e1f84d-4529-47f9-807c-7e66a66f9f4b",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders) # put sum(customer_orders) before but as a set of strings it can not be calculated\n",
"\n",
" percentage_unique_products = (len(customer_orders) / len(products)) * 100 #(quantidade produto ordered / quantidade total do produto) * 100\n",
"\n",
" return total_products_ordered, percentage_unique_products"
]
},
{
"cell_type": "markdown",
"id": "9e316eec-e41c-466b-afa9-e11402210f5e",
"metadata": {},
"source": [
"5. Define a function named print_order_statistics that takes order_statistics as a parameter."
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "29776d40-1bac-4c9f-8512-be6c11b44298",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(orders_statistics):\n",
"\n",
" total_products_ordered, percentage_unique_products = order_statistics # tuple for example (5, 30) = order_statistics\n",
"\n",
" print(f\"The total of products ordered is: {total_products_ordered}\")\n",
" print(f\"The percentage of unique products ordered is: {percentage_unique_products}\")"
]
},
{
"cell_type": "markdown",
"id": "e794a8fc-45ac-4f53-ab22-d1e97a171483",
"metadata": {},
"source": [
"6. Define a function named print_updated_inventory that takes inventory as a parameter."
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "0e788f6d-9079-43ec-a90c-bbeca42d0758",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" \n",
" print(f\"The updated inventory is: {inventory}\")"
]
},
{
"cell_type": "markdown",
"id": "3cfc5804-f755-433b-bb6e-8812b4b8d6c9",
"metadata": {},
"source": [
"7. Call the functions in the appropriate sequence to execute the program and manage customer orders."
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "345b4a99-3e61-4c6a-839b-2af5d0dcef7b",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity for t-shirt: 45\n",
"Enter the quantity for mug: 23\n",
"Enter the quantity for hat: 9\n",
"Enter the quantity for book: 21\n",
"Enter the quantity for keychain: 4\n",
"Enter your order: t-shirt\n",
"Do you want to enter place another order? (yes/no): yes\n",
"Enter your order: book\n",
"Do you want to enter place another order? (yes/no): no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The total of products ordered is: 2\n",
"The percentage of unique products ordered is: 40.0\n",
"The updated inventory is: {'t-shirt': 44, 'mug': 23, 'hat': 9, 'book': 20, 'keychain': 4}\n"
]
}
],
"source": [
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"\n",
"updated_inventory = update_inventory(customer_orders, inventory)\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory(updated_inventory)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:anaconda3]",
"language": "python",
"name": "conda-env-anaconda3-py"
},
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading