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
69 changes: 69 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"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"
]
}
],
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
113 changes: 113 additions & 0 deletions LAB-functions.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"id": "ac56d07d-40a2-43cb-a7bd-7f0627f9201a",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the quantity of tshirt: 20\n",
"Please enter the quantity of mug: 20\n",
"Please enter the quantity of hat: 20\n",
"Please enter the quantity of book: 20\n",
"Please enter the quantity of keychain: 20\n",
"Please enter the product that you would like to order: tshirt\n",
"Would you like to add another product? Y/N Y\n",
"Please enter the product that you would like to order: mug\n",
"Would you like to add another product? Y/N Y\n",
"Please enter the product that you would like to order: book\n",
"Would you like to add another product? Y/N N\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"(3, 60.0)\n",
"{'tshirt': 19, 'mug': 19, 'hat': 20, 'book': 19, 'keychain': 20}\n"
]
}
],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for p in products:\n",
" user_input1 = int(input(f\"Please enter the quantity of {p}: \"))\n",
" inventory[p] = user_input1\n",
" return inventory\n",
"\n",
"\n",
"def get_customer_orders():\n",
" customer_order = set()\n",
" user_input2 = input(\"Please enter the product that you would like to order: \")\n",
" customer_order.add(user_input2)\n",
" user_input3 = input(\"Would you like to add another product? Y/N\")\n",
" while user_input3 == \"Y\":\n",
" user_input2 = input(\"Please enter the product that you would like to order: \")\n",
" customer_order.add(user_input2)\n",
" user_input3 = input(\"Would you like to add another product? Y/N\")\n",
" return customer_order\n",
"\n",
"\n",
"def calculate_order_statistics(customer_order, products):\n",
" total_products_ordered = len(customer_order)\n",
" percentage = (total_products_ordered / len(products)) * 100\n",
" return total_products_ordered, percentage\n",
"\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" total_products_ordered, percentage = order_statistics\n",
" print (order_statistics) \n",
"\n",
"\n",
"def print_updated_inventory(inventory):\n",
" for i in customer_order:\n",
" if i in inventory:\n",
" inventory[i] = inventory[i]-1\n",
" print(inventory) \n",
"\n",
"\n",
"products = [\"tshirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory=initialize_inventory(products)\n",
"customer_order=get_customer_orders()\n",
"order_statistics=calculate_order_statistics(customer_order, products)\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9c3773a4-d956-49c2-90f5-6f782ac2f4f9",
"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.12.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
2 changes: 1 addition & 1 deletion lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.10"
}
},
"nbformat": 4,
Expand Down