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
158 changes: 155 additions & 3 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,165 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "9eec74b5-c3f9-499c-bee2-eb82f53407c5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the quantity available for each product:\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"t-shirt: 10\n",
"mug: 10\n",
"hat: 10\n",
"book: 10\n",
"keychain: 10\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product to order: hat\n",
"Do you want to add another product? (yes/no): yes\n",
"Enter a product to order: mug\n",
"Do you want to add another product? (yes/no): no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 40.00%\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 10\n",
"mug: 9\n",
"hat: 9\n",
"book: 10\n",
"keychain: 10\n"
]
}
],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
" print(\"Enter the quantity available for each product:\")\n",
" for product in products:\n",
" quantity = int(input(f\"{product}: \"))\n",
" inventory[product] = quantity\n",
" return inventory\n",
"\n",
"\n",
"# customer orders\n",
"def get_customer_orders(products):\n",
" customer_orders = set()\n",
" print(\"\\nAvailable products:\", products)\n",
"\n",
" while True:\n",
" product_choice = input(\"Enter a product to order: \").lower()\n",
" if product_choice in products:\n",
" customer_orders.add(product_choice)\n",
" else:\n",
" print(\"Invalid product, please choose from the list.\")\n",
"\n",
" another = input(\"Do you want to add another product? (yes/no): \").lower()\n",
" if another != \"yes\":\n",
" break\n",
"\n",
" return customer_orders\n",
"\n",
"\n",
"# update inventory based on customer orders\n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" inventory[product] -= 1\n",
" return inventory\n",
"\n",
"\n",
"# calculate order statistics\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" total_products_available = len(products)\n",
" percentage_ordered = (total_products_ordered / total_products_available) * 100\n",
" return (total_products_ordered, percentage_ordered)\n",
"\n",
"\n",
"# print order statistics\n",
"def print_order_statistics(order_statistics):\n",
" print(\"\\nOrder Statistics:\")\n",
" print(f\"Total Products Ordered: {order_statistics[0]}\")\n",
" print(f\"Percentage of Products Ordered: {order_statistics[1]:.2f}%\")\n",
"\n",
"\n",
"# print the updated inventory\n",
"def print_updated_inventory(inventory):\n",
" print(\"\\nUpdated Inventory:\")\n",
" for product in inventory:\n",
" print(f\"{product}: {inventory[product]}\")\n",
"\n",
"\n",
"# program\n",
"\n",
"def main():\n",
" products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
" # initialize inventory\n",
" inventory = initialize_inventory(products)\n",
"\n",
" # get customer orders\n",
" customer_orders = get_customer_orders(products)\n",
"\n",
" # update inventory\n",
" inventory = update_inventory(customer_orders, inventory)\n",
"\n",
" # calculate statistics\n",
" order_statistics = calculate_order_statistics(customer_orders, products)\n",
"\n",
" # print results\n",
" print_order_statistics(order_statistics)\n",
" print_updated_inventory(inventory)\n",
"\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "87fcbf5f-4958-47c2-ba3b-82607648628c",
"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 +213,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down