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
295 changes: 295 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
{
"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": 10,
"id": "5f1fc8da-baba-4304-af32-8e51980ca275",
"metadata": {},
"outputs": [],
"source": [
"# List of products\n",
"products = ['t-shirt', 'mug', 'hat', 'book', 'keychain']"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "204742ee-8867-4710-aacd-7d0ab96a8aa2",
"metadata": {},
"outputs": [],
"source": [
"# Empty inventory dict\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "5af3a28c-8127-45dc-880a-691fa9713783",
"metadata": {},
"outputs": [],
"source": [
"# Empty set for customer_orders\n",
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "787af789-2dd0-490a-8858-95d4246000ed",
"metadata": {},
"outputs": [],
"source": [
"# 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",
" for product in products:\n",
" value = int(input(f'How many items of {product} is in inventory?'))\n",
" inventory[product] = value\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "96df1246-95a9-400c-976c-a81b79310b2b",
"metadata": {},
"outputs": [],
"source": [
"# 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",
" item_order = str(input('Which item client want to order?'))\n",
" while item_order not in products:\n",
" print('Invalid product. Write an item from products')\n",
" item_order = str(input('Which item client want to order?'))\n",
" customer_orders.add(item_order)\n",
" \n",
" order_more = str(input('Does the client wnat to order more? Y/N')).upper()\n",
" while order_more not in ['Y', 'N']:\n",
" print('Invalid input. Please type Y or N')\n",
" order_more = str(input('Does the client want to order more? Y/N ')).upper() \n",
" \n",
" while order_more == ('Y'):\n",
" item_order = str(input('Which item client want to order?'))\n",
" while item_order not in products:\n",
" print('Invalid product. Write an item from products')\n",
" item_order = str(input('Which item client want to order?'))\n",
" customer_orders.add(item_order)\n",
" order_more = str(input('Does the client wnat to order more? Y/N')).upper()\n",
" if order_more == ('N'):\n",
" print('Order Finished') \n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "cc1bc12a-d830-4766-911a-e3f356f780ab",
"metadata": {},
"outputs": [],
"source": [
"# 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",
" for product in customer_orders:\n",
" inventory[product] -= 1\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "409c6ee4-5e84-4ee1-a32b-09030a9b2b65",
"metadata": {},
"outputs": [],
"source": [
"# 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",
" perc_products_ordered = (len(customer_orders) / len(products)) * 100\n",
" return total_products_ordered, perc_products_ordered\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "5ca2f727-95fb-44db-92b0-058ff0e2d065",
"metadata": {},
"outputs": [],
"source": [
"# 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",
" print(f'Total products ordered: {order_statistics[0]}')\n",
" print(f'Percentage of products ordered: {order_statistics[1]}')"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "a2138dde-5ede-4991-950d-0abaaab3b85b",
"metadata": {},
"outputs": [],
"source": [
"# 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",
" for product, quantity in inventory.items():\n",
" print(f'{product} : {quantity}')"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "fdc6c421-a827-4705-b87c-3ce3e73ee55d",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"How many items of t-shirt is in inventory? 1\n",
"How many items of mug is in inventory? 2\n",
"How many items of hat is in inventory? 3\n",
"How many items of book is in inventory? 4\n",
"How many items of keychain is in inventory? 5\n",
"Which item client want to order? hook\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid product. Write an item from products\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Which item client want to order? book\n",
"Does the client wnat to order more? Y/N y\n",
"Which item client want to order? hat\n",
"Does the client wnat to order more? Y/N y\n",
"Which item client want to order? keychain\n",
"Does the client wnat to order more? Y/N n\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Finished\n",
"Total products ordered: 3\n",
"Percentage of products ordered: 60.0\n",
"t-shirt : 1\n",
"mug : 2\n",
"hat : 2\n",
"book : 3\n",
"keychain : 4\n"
]
}
],
"source": [
"# Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"\n",
"initialize_inventory(products)\n",
"\n",
"get_customer_orders()\n",
"\n",
"update_inventory(customer_orders, inventory)\n",
"\n",
"calculate_order_statistics(customer_orders, products)\n",
"\n",
"print_order_statistics(order_statistics)\n",
"\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cbec6fe4-e90b-41a6-98e0-26a639880b5d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading