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
257 changes: 255 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,264 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "cf69fd28",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 200, 'mug': 200, 'hat': 200, 'book': 200, 'keychain': 200}\n",
"{'t-shirt', 'mug'}\n",
"Order Statistics:\n",
"Total Products Ordered: <2>.\n",
"Percentage of Products Ordered: <40.0>%\n",
"This is the upadted inventory: \n",
"T-shirt: 199\n",
"Mug: 199\n",
"Hat: 200\n",
"Book: 200\n",
"Keychain: 200\n",
"\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products):\n",
" \"\"\"Initialize an inventory in a dictionary using a loop and user input.\"\"\"\n",
" inventory = {}\n",
" for item in products:\n",
" quantity_of_item = int(input(f'Enter the quantity of {item}: '))\n",
" inventory[item] = quantity_of_item\n",
" return inventory\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"print(inventory)\n",
"\n",
"def get_customer_orders():\n",
" \"\"\"Prompt the user to enter the product names using a loop and return customer_orders set.\"\"\"\n",
" customer_orders = set()\n",
" while True:\n",
" user_input = input(f'Enter the name of a product from {products}')\n",
" customer_orders.add(user_input)\n",
" question = input(f'Do you want to add another product (yes/no): ').lower()\n",
" if question == 'no':\n",
" break\n",
" return customer_orders\n",
"\n",
"customer_orders = get_customer_orders()\n",
"\n",
"print(customer_orders)\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" \"\"\"It takes customer_orders and products and it calculates the order statistics.\"\"\"\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_of_prodcuts_ordered = total_products_ordered/len(products)*100\n",
" return (total_products_ordered, percentage_of_prodcuts_ordered)\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" \"\"\"It takes `order_statistics` as a parameter and it prints the order statistics.\"\"\"\n",
" print(\n",
" f'Order Statistics:\\n'\n",
" f'Total Products Ordered: <{order_statistics[0]}>.\\n'\n",
" f'Percentage of Products Ordered: <{order_statistics[1]}>%'\n",
" )\n",
"\n",
"print_order_statistics(order_statistics)\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" \"\"\"It takes customer_orders and inventory and it updates the inventory dictionary based on the customer orders.\"\"\"\n",
" for item in customer_orders:\n",
" for product in products:\n",
" if item == product:\n",
" inventory[item] -= 1\n",
" return inventory\n",
"\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"\n",
"def print_updated_inventory(inventory):\n",
" \"\"\"It takes inventory and it prints the updated inventory.\"\"\"\n",
" print(\n",
" f'This is the upadted inventory: \\n'\n",
" f'T-shirt: {inventory[\"t-shirt\"]}\\n'\n",
" f'Mug: {inventory[\"mug\"]}\\n'\n",
" f'Hat: {inventory[\"hat\"]}\\n'\n",
" f'Book: {inventory[\"book\"]}\\n'\n",
" f'Keychain: {inventory[\"keychain\"]}\\n'\n",
" )\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "5e119d2d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 200, 'mug': 200, 'hat': 200, 'book': 200, 'keychain': 200}\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products):\n",
" \"\"\"Initialize an inventory in a dictionary using a loop and user input.\"\"\"\n",
" inventory = {}\n",
" for item in products:\n",
" quantity_of_item = int(input(f'Enter the quantity of {item}: '))\n",
" inventory[item] = quantity_of_item\n",
" return inventory\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "f5dc1f6f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt', 'mug'}\n"
]
}
],
"source": [
"def get_customer_orders():\n",
" \"\"\"Prompt the user to enter the product names using a loop and return customer_orders set.\"\"\"\n",
" customer_orders = set()\n",
" while True:\n",
" user_input = input(f'Enter the name of a product from {products}')\n",
" customer_orders.add(user_input)\n",
" question = input(f'Do you want to add another product (yes/no): ').lower()\n",
" if question == 'no':\n",
" break\n",
" return customer_orders\n",
"\n",
"customer_orders = get_customer_orders()\n",
"\n",
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "7be95ddd",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" \"\"\"It takes customer_orders and products and it calculates the order statistics.\"\"\"\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_of_prodcuts_ordered = total_products_ordered/len(products)*100\n",
" return (total_products_ordered, percentage_of_prodcuts_ordered)\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "08a9d4a9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: <2>.\n",
"Percentage of Products Ordered: <40.0>%\n"
]
}
],
"source": [
"def print_order_statistics(order_statistics):\n",
" \"\"\"It takes `order_statistics` as a parameter and it prints the order statistics.\"\"\"\n",
" print(\n",
" f'Order Statistics:\\n'\n",
" f'Total Products Ordered: <{order_statistics[0]}>.\\n'\n",
" f'Percentage of Products Ordered: <{order_statistics[1]}>%'\n",
" )\n",
"\n",
"print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "ae2e34b1",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" \"\"\"It takes customer_orders and inventory and it updates the inventory dictionary based on the customer orders.\"\"\"\n",
" for item in customer_orders:\n",
" for product in products:\n",
" if item == product:\n",
" inventory[item] -= 1\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "33cc303c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is the upadted inventory: \n",
"T-shirt: 199\n",
"Mug: 199\n",
"Hat: 200\n",
"Book: 200\n",
"Keychain: 200\n",
"\n"
]
}
],
"source": [
"inventory = update_inventory(customer_orders, inventory)\n",
"\n",
"def print_updated_inventory(inventory):\n",
" \"\"\"It takes inventory and it prints the updated inventory.\"\"\"\n",
" print(\n",
" f'This is the upadted inventory: \\n'\n",
" f'T-shirt: {inventory[\"t-shirt\"]}\\n'\n",
" f'Mug: {inventory[\"mug\"]}\\n'\n",
" f'Hat: {inventory[\"hat\"]}\\n'\n",
" f'Book: {inventory[\"book\"]}\\n'\n",
" f'Keychain: {inventory[\"keychain\"]}\\n'\n",
" )\n",
"print_updated_inventory(inventory)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +314,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down