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
330 changes: 327 additions & 3 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,337 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "c2be4124-9ebb-41b9-8d1b-ea0f4f46c282",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the qty t-shirt:, 20\n",
"Enter the qty mug:, 34\n",
"Enter the qty hat:, 16\n",
"Enter the qty book:, 36\n",
"Enter the qty keychain:, 36\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The final list of inventory: {'t-shirt': 20, 'mug': 34, 'hat': 16, 'book': 36, 'keychain': 36}\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"] #Define a function named initialize_inventory that takes products as a parameter.\n",
"def initialize_inventory(products: list[str]):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(f\"Enter the qty {product}:, \"))\n",
" inventory[product] = quantity\n",
" return inventory\n",
"\n",
"inventory = initialize_inventory(products)\n",
"print(\"The final list of inventory:\", inventory)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "bce1b898-ba91-442a-a543-fa727fc1718f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The list of products ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the item to order:, mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"mug added to your order\n",
"The list of products ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the item to order:, hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"hat added to your order\n",
"The list of products ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the item to order:, 0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The final order: {'hat', 'mug'}\n"
]
}
],
"source": [
"def get_customer_orders(): #Define a function named get_customer_orders that takes no parameters\n",
" customer_orders = set()\n",
" while True:\n",
" print(\"The list of products\", products)\n",
" product = input(f\"Enter the item to order:, \").lower()\n",
" if product == \"0\":\n",
" break\n",
" elif product in products:\n",
" customer_orders.add(product)\n",
" print(f\"{product} added to your order\")\n",
" else:\n",
" print(\"Invalid product\")\n",
" return customer_orders\n",
"\n",
"orders = get_customer_orders()\n",
"print(\"The final order:\", orders) "
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "fe0f8c28-602e-40bd-a649-a2e4ccbb4d1d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The updated inventory is: {'t-shirt': 20, 'mug': 33, 'hat': 15, 'book': 36, 'keychain': 36}\n"
]
}
],
"source": [
"def update_inventory(customer_orders, inventory): #function to update inventory\n",
" for product in customer_orders:\n",
" if product in inventory and inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" return inventory\n",
"\n",
"print(\"The updated inventory is:\",update_inventory(orders, inventory)) "
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "c32f1b58-2328-43da-ac74-b97752e7805b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The total number of products ordered are: 2\n",
"The % of products ordered are: 40.0 %\n"
]
}
],
"source": [
"def calculate_order_statistics(customer_orders, inventory): #order statistics\n",
" total_products_ordered = len(customer_orders) #total product ordered\n",
" percentage_total_products_ordered = (total_products_ordered / len(products)) * 100 #percentage of total products ordered\n",
" return total_products_ordered, percentage_total_products_ordered\n",
"total, percentage= calculate_order_statistics(orders, inventory)\n",
"print(\"The total number of products ordered are:\",total)\n",
"print(\"The % of products ordered are:\", percentage, \"%\")"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "12a66805-2958-4f9c-864d-55bae314445e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The total number of products ordered are: 2\n",
"The % of products ordered are: 40.0 %\n"
]
}
],
"source": [
"def print_order_statistics(order_statistics): #printing order stastics\n",
" total_products_ordered, percentage_total_products_ordered = order_statistics\n",
" print(\"The total number of products ordered are:\",total_products_ordered)\n",
" print(\"The % of products ordered are:\", percentage_total_products_ordered, \"%\")\n",
"order_statistics = calculate_order_statistics(orders, inventory)\n",
"print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "c888f863-37d2-42e1-b5eb-7f56e9ab96be",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated Inventory:\n",
"t-shirt: 20\n",
"mug: 33\n",
"hat: 15\n",
"book: 36\n",
"keychain: 36\n"
]
}
],
"source": [
"def print_updated_inventory(inventory): #printing updated inventory\n",
" print(\"Updated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "6631bcb7-5ff2-43c6-8eb2-3186eadeb831",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the qty t-shirt:, 50\n",
"Enter the qty mug:, 50\n",
"Enter the qty hat:, 50\n",
"Enter the qty book:, 50\n",
"Enter the qty keychain:, 50\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The list of products ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the item to order:, mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"mug added to your order\n",
"The list of products ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the item to order:, hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"hat added to your order\n",
"The list of products ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the item to order:, book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"book added to your order\n",
"The list of products ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the item to order:, 0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The total number of products ordered are: 3\n",
"The % of products ordered are: 60.0 %\n",
"Updated Inventory:\n",
"t-shirt: 50\n",
"mug: 49\n",
"hat: 49\n",
"book: 49\n",
"keychain: 50\n"
]
}
],
"source": [
"inventory = initialize_inventory(products)\n",
"orders = get_customer_orders()\n",
"update_inventory(orders, inventory)\n",
"calculate_order_statistics(orders, inventory)\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a242da4d-806b-4047-be7a-0ba3be314890",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -61,7 +385,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down