Skip to content

lab solved #429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
362 changes: 362 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,362 @@
{
"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": 117,
"id": "0d0b9f32-4d47-4fd8-817a-cd84ebe2b848",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"How many t-shirts are in the inventory? 5\n",
"How many mugs are in the inventory? 5\n",
"How many hats are in the inventory? 5\n",
"How many books are in the inventory? 5\n",
"How many keychains are in the inventory? 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n"
]
}
],
"source": [
"#Inventory details\n",
"products = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"\n",
"#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",
"\n",
"def initialize_inventory (products):\n",
" inventory = {}\n",
" for product in products:\n",
" inventory[product] = int(input(f\"How many {product}s are in the inventory?\"))\n",
" return inventory\n",
"\n",
"\n",
"inventory = initialize_inventory(products)\n",
"print(inventory)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 119,
"id": "9719fecf-5d93-4823-98f3-2404845605e9",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to order a product? (yes/no): yes\n",
"Enter the product you want to order: mug\n",
"Do you want to order a product? (yes/no): yes\n",
"Enter the product you want to order: hat\n",
"Do you want to order a product? (yes/no): no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'hat', 'mug'}\n"
]
}
],
"source": [
"#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. \n",
"#The function should return the customer_orders set.\n",
"\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" while True:\n",
" order = input(\"Do you want to order a product? (yes/no): \")\n",
" if order == \"yes\":\n",
" product = input(\"Enter the product you want to order: \")\n",
" customer_orders.add(product)\n",
" elif order == \"no\":\n",
" break\n",
" else:\n",
" print(\"Invalid input. Please answer with 'yes' or 'no'\")\n",
" \n",
" return customer_orders\n",
"\n",
"customer_orders = get_customer_orders()\n",
"print(customer_orders)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 123,
"id": "2828a0b0-d991-4d12-abc8-bf0a0f7de17a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n",
"{'hat', 'mug'}\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",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" if inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" elif product in inventory and inventory[product] <= 0:\n",
" print(f\"There is not enough {product} on stock to fulfill the order!\")\n",
"\n",
" return inventory\n",
"\n",
"print(inventory)\n",
"print(customer_orders)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 125,
"id": "10f53296-bed3-4718-8b63-c9247dca9794",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"8%\n"
]
}
],
"source": [
"#4 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_ordered = len(customer_orders)\n",
" total_inventory = sum(inventory.values())\n",
" percentage_ordered = (total_ordered / total_inventory) * 100\n",
"\n",
" return total_ordered, percentage_ordered\n",
"\n",
"total_ordered, percentage_ordered = calculate_order_statistics(customer_orders, products)\n",
"\n",
"print(total_ordered)\n",
"print(f\"{round(percentage_ordered)}%\")\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 127,
"id": "4b615619-33ce-44e4-b00d-3d0465c44eae",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 8%\n"
]
}
],
"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",
" print(\"Order Statistics:\")\n",
" print(f\"Total Products Ordered: {total_ordered}\")\n",
" print(f\"Percentage of Products Ordered: {round(percentage_ordered)}%\")\n",
" return\n",
"\n",
"print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 129,
"id": "42bd3dcb-cff7-4d8b-abb5-998a2726f948",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated inventory after the order: {'t-shirt': 5, 'mug': 4, 'hat': 4, 'book': 5, 'keychain': 5}\n"
]
}
],
"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 in customer_orders:\n",
" if product in inventory and inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" elif product in inventory and inventory[product] <= 0:\n",
" print(f\"There is not enough {product} on stock to fulfill the order!\")\n",
"\n",
" print(f\"Updated inventory after the order: {inventory}\")\n",
" return\n",
"\n",
"print_updated_inventory(inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": 131,
"id": "5139693e-9edb-4db4-9f85-813f309d5bc2",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"How many t-shirts are in the inventory? 5\n",
"How many mugs are in the inventory? 6\n",
"How many hats are in the inventory? 8\n",
"How many books are in the inventory? 3\n",
"How many keychains are in the inventory? 7\n",
"Do you want to order a product? (yes/no): 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input. Please answer with 'yes' or 'no'\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to order a product? (yes/no): yes\n",
"Enter the product you want to order: hat\n",
"Do you want to order a product? (yes/no): yes\n",
"Enter the product you want to order: mug\n",
"Do you want to order a product? (yes/no): no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 8%\n",
"Updated inventory: {'t-shirt': 5, 'mug': 5, 'hat': 7, 'book': 3, 'keychain': 7}\n"
]
}
],
"source": [
"#7 Functions:\n",
"\n",
"products = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"customer_orders = get_customer_orders()\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"\n",
"print_order_statistics(order_statistics)\n",
"\n",
"update_inventory(customer_orders, inventory)\n",
"\n",
"print(\"Updated inventory:\", inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cf5c4c3b-0d5b-45bd-a650-6a2caa261af9",
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading