Skip to content
Open
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
242 changes: 241 additions & 1 deletion lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,246 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "d40ba57c-a35b-4891-8220-bf7064baff0b",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the quantity of t-shirt: 20\n",
"Please enter the quantity of mug: 5\n",
"Please enter the quantity of hat: 10\n",
"Please enter the quantity of book: 43\n",
"Please enter the quantity of keychain: 21\n"
]
}
],
"source": [
"# 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",
"products=['t-shirt','mug','hat','book','keychain']\n",
"\n",
"def initialize_inventory(products):\n",
" inventory={}\n",
" for product in products:\n",
" qty=int(input(f'Please enter the quantity of {product}:'))\n",
" inventory[product]=qty\n",
" return inventory\n",
"\n",
"inventory=initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "ab47d4cf-0dc5-4d01-a9de-a90f74024d34",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of the product you want to order: keychain\n",
"Do you want to add another product? Answer only with 'yes' or 'no': yes\n",
"Enter the name of the product you want to order: book\n",
"Do you want to add another product? Answer only with 'yes' or 'no': yes\n",
"Enter the name of the product you want to order: hat\n",
"Do you want to add another product? Answer only with 'yes' or 'no': yes\n",
"Enter the name of the product you want to order: mug\n",
"Do you want to add another product? Answer only with 'yes' or 'no': no\n"
]
}
],
"source": [
"# 2. Define a function named `get_customer_orders` that takes no parameters. Inside the function, implement the code for prompting the user \n",
"# to enter the product names using a loop. The function should return the `customer_orders` set.\n",
"\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" answer = 'yes'\n",
" while answer == 'yes':\n",
" order = input('Enter the name of the product you want to order: ')\n",
" customer_orders.add(order)\n",
" answer = input(\"Do you want to add another product? Answer only with 'yes' or 'no': \")\n",
" return customer_orders\n",
"\n",
"customer_orders=get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "37f4e893-0390-400c-814e-fc61223ee059",
"metadata": {},
"outputs": [],
"source": [
"# 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",
"def update_inventory(customer_orders, inventory):\n",
" for order in customer_orders:\n",
" if order in inventory:\n",
" inventory[order] -= 1\n",
" return inventory\n",
"\n",
"inventory=update_inventory(customer_orders, inventory)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "40e88667-04e3-419c-9e92-c6cc4e251f7b",
"metadata": {},
"outputs": [],
"source": [
"# 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",
"def calculate_order_statistics(customer_orders, products):\n",
" total_ordered = len(customer_orders)\n",
" percentage_ordered = (len(customer_orders) / len(products)) * 100\n",
" return total_ordered,percentage_ordered\n",
"\n",
"order_statistics=calculate_order_statistics(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "140af5af-2797-4121-b9d3-f2f28f0ab7e1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(4, 80.0)\n"
]
}
],
"source": [
"# 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",
"def print_order_statistics(order_statistics):\n",
" print(order_statistics)\n",
"\n",
"print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "da8a437d-003a-49b2-8cbc-2b758b52d67c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 20, 'mug': 4, 'hat': 9, 'book': 42, 'keychain': 20}\n"
]
}
],
"source": [
"# 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",
"def print_updated_inventory(inventory):\n",
" print(inventory)\n",
"\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "dc744a02-4ba5-4e91-a76e-de6233c4cd3b",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the quantity of t-shirt: 20\n",
"Please enter the quantity of mug: 43\n",
"Please enter the quantity of hat: 56\n",
"Please enter the quantity of book: 9\n",
"Please enter the quantity of keychain: 5\n",
"Enter the name of the product you want to order: mug\n",
"Do you want to add another product? Answer only with 'yes' or 'no': yes\n",
"Enter the name of the product you want to order: hat\n",
"Do you want to add another product? Answer only with 'yes' or 'no': book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"(2, 40.0)\n",
"{'t-shirt': 20, 'mug': 42, 'hat': 55, 'book': 9, 'keychain': 5}\n"
]
}
],
"source": [
"# 7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"\n",
"products=['t-shirt','mug','hat','book','keychain']\n",
"\n",
"def initialize_inventory(products):\n",
" inventory={}\n",
" for product in products:\n",
" qty=int(input(f'Please enter the quantity of {product}:'))\n",
" inventory[product]=qty\n",
" return inventory\n",
"\n",
"inventory=initialize_inventory(products)\n",
"\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" answer = 'yes'\n",
" while answer == 'yes':\n",
" order = input('Enter the name of the product you want to order: ')\n",
" customer_orders.add(order)\n",
" answer = input(\"Do you want to add another product? Answer only with 'yes' or 'no': \")\n",
" return customer_orders\n",
"\n",
"customer_orders=get_customer_orders()\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" for order in customer_orders:\n",
" if order in inventory:\n",
" inventory[order] -= 1\n",
" return inventory\n",
"\n",
"inventory=update_inventory(customer_orders, inventory)\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_ordered = len(customer_orders)\n",
" percentage_ordered = (len(customer_orders) / len(products)) * 100\n",
" return total_ordered,percentage_ordered\n",
"\n",
"order_statistics=calculate_order_statistics(customer_orders, products)\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" print(order_statistics)\n",
"\n",
"print_order_statistics(order_statistics)\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(inventory)\n",
"\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "be31fccd-fa49-4056-a3bb-dce5041f70bf",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -61,7 +301,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.4"
}
},
"nbformat": 4,
Expand Down