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
314 changes: 314 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,314 @@
{
"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": 1,
"id": "26fb7fd9-f2e4-4c82-83d5-3fa69201f141",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"How many of hat would you like to purchase (put zero if you wish not to buy the item)? 5\n",
"How many of mug would you like to purchase (put zero if you wish not to buy the item)? 3\n",
"How many of keychain would you like to purchase (put zero if you wish not to buy the item)? 4\n",
"How many of book would you like to purchase (put zero if you wish not to buy the item)? 2\n",
"How many of t-shirt would you like to purchase (put zero if you wish not to buy the item)? 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'hat': 5, 'mug': 3, 'keychain': 4, 'book': 2, 't-shirt': 1}\n"
]
}
],
"source": [
"#1. 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",
"def initialize_inventory(products) :\n",
" inventory = {}\n",
" for product_name in products :\n",
" quantity = int (input (f\"How many of {product_name} would you like to purchase (put zero if you wish not to buy the item)?\"))\n",
" inventory [product_name] = quantity\n",
" return inventory \n",
"products = [\"hat\", \"mug\", \"keychain\", \"book\", \"t-shirt\"]\n",
"print (initialize_inventory(products))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "0a391b7d-bfc6-494a-a8d5-0d0832de5891",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Which items would you like to checkout? (hat, mug, book, keychain, t-shirt, separated by comma) mug, book, hat\n",
"Have you forgotten anything? (yes/no) no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Thank you for your order!\n",
"{' book', ' hat', 'mug'}\n"
]
}
],
"source": [
"#2.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",
"def get_customer_orders ():\n",
" customer_orders = set ()\n",
" #initialialize_inventory (products)\n",
" for product_name in products :\n",
" items_list = input (\"Which items would you like to checkout? (hat, mug, book, keychain, t-shirt, separated by comma)\").split(\",\")\n",
" customer_orders.update(items_list)\n",
" response = input (\"Have you forgotten anything? (yes/no)\")\n",
" if response == \"no\":\n",
" print (\"Thank you for your order!\")\n",
" else : (input (\"Please specify the item\")) \n",
" break \n",
" return customer_orders \n",
"print (get_customer_orders())"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "2ff921f0-b87e-44d2-bb54-0cdd5ead32f2",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Which items would you like to checkout? (hat, mug, book, keychain, t-shirt, separated by comma) book, mug\n",
"Have you forgotten anything? (yes/no) no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Thank you for your order!\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"How many of hat would you like to purchase (put zero if you wish not to buy the item)? 3\n",
"How many of mug would you like to purchase (put zero if you wish not to buy the item)? 3\n",
"How many of keychain would you like to purchase (put zero if you wish not to buy the item)? 0\n",
"How many of book would you like to purchase (put zero if you wish not to buy the item)? 0\n",
"How many of t-shirt would you like to purchase (put zero if you wish not to buy the item)? 6\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'hat': 3, 'mug': 3, 'keychain': 0, 'book': -1, 't-shirt': 6}\n"
]
}
],
"source": [
"#3 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",
"customer_orders = get_customer_orders()\n",
"inventory = initialize_inventory (products)\n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1 \n",
" return inventory\n",
"\n",
"print (update_inventory (customer_orders,inventory))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "3796a97b-cdae-4fd3-8505-e4d9caf3d822",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(2, 40.0)\n"
]
}
],
"source": [
"#4 Define a function named calculate_order_statistics that takes customer_orders and products as parameters. Inside the function, \n",
"#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",
"def calculate_order_statistics(customer_orders, products):\n",
" total_ordered = len(customer_orders)\n",
" unique_percent = (total_ordered / len(products)) * 100\n",
" return total_ordered, unique_percent\n",
"print (calculate_order_statistics(customer_orders, products))"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "5d6a1731-28b6-4521-859e-0d08cd8780c2",
"metadata": {},
"outputs": [],
"source": [
"#5. 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",
" total, percent = order_statistics\n",
" print(f\"Total Products Ordered: {total}\")\n",
" print(f\"Percentage of Unique Products Ordered: {percent:.2f}%\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "e4376fe3-d443-4ff5-bf9e-2f775747ffb1",
"metadata": {},
"outputs": [],
"source": [
"#6. 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",
"def print_updated_inventory(inventory):\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity} remaining\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "80c8bdc8-235b-46ff-b9cb-bd0de7732551",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"How many of t-shirt would you like to purchase (put zero if you wish not to buy the item)? 3\n",
"How many of mug would you like to purchase (put zero if you wish not to buy the item)? 3\n",
"How many of hat would you like to purchase (put zero if you wish not to buy the item)? 5\n",
"How many of book would you like to purchase (put zero if you wish not to buy the item)? 6\n",
"How many of keychain would you like to purchase (put zero if you wish not to buy the item)? 0\n",
"Which items would you like to checkout? (hat, mug, book, keychain, t-shirt, separated by comma) hat,mug,book\n",
"Have you forgotten anything? (yes/no) no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Thank you for your order!\n",
"Total Products Ordered: 3\n",
"Percentage of Unique Products Ordered: 60.00%\n",
"t-shirt: 3 remaining\n",
"mug: 2 remaining\n",
"hat: 4 remaining\n",
"book: 5 remaining\n",
"keychain: 0 remaining\n"
]
}
],
"source": [
"#7 Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"def all():\n",
" products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
" inventory = initialize_inventory(products)\n",
" customer_orders = get_customer_orders()\n",
" updated_inventory = update_inventory(customer_orders, inventory)\n",
" order_statistics = calculate_order_statistics(customer_orders, products)\n",
" print_order_statistics(order_statistics)\n",
" print_updated_inventory(updated_inventory)\n",
"\n",
"all()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "09a60faf-22e9-4f7e-af61-a71129c292e6",
"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