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
100 changes: 99 additions & 1 deletion lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,104 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f502dfc4-a993-4009-8542-4cf84d5f0e55",
"metadata": {},
"outputs": [],
"source": [
"# list of products\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"\n",
"# 1. Defining a function initialize_inventory\n",
"def initialize_inventory(products):\n",
" \"\"\"Initializes the inventory dictionary using a loop and user input.\"\"\"\n",
" print(\"____Inventory Setup ____\")\n",
" inventory = {}\n",
" for product in products:\n",
" inventory[product] = int(input(f\"Enter quantity for {product}: \"))\n",
" return inventory\n",
"\n",
"\n",
"# 2. Defining function get_customer_orders\n",
"def get_customer_orders():\n",
" \"\"\"Prompts the user to enter product names using a loop and returns the set.\"\"\"\n",
" print(\"\\n___ Place Customer Order ___\")\n",
" customer_orders = set()\n",
" while True:\n",
" ordered_item = input(\"Enter the name of a product to order: \").strip().lower()\n",
" customer_orders.add(ordered_item)\n",
" \n",
" another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n",
" if another != 'yes':\n",
" break\n",
" return customer_orders\n",
"\n",
"\n",
"# 3. Define function update_inventory\n",
"def update_inventory(customer_orders, inventory):\n",
" \"\"\"Updates the inventory dictionary based on the customer orders.\"\"\"\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1\n",
"\n",
"\n",
"# 4. Define calculate_order_statistics\n",
"def calculate_order_statistics(customer_orders, products):\n",
" \"\"\"Calculates total products ordered and percentage of unique products ordered.\"\"\"\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = (total_products_ordered / len(products)) * 100\n",
" return total_products_ordered, percentage_ordered\n",
"\n",
"\n",
"# 5. Define print_order_statistics\n",
"def print_order_statistics(order_statistics):\n",
" \"\"\"Prints the calculated order statistics.\"\"\"\n",
" total, percentage = order_statistics\n",
" print(\"\\nOrder Statistics:\")\n",
" print(f\"Total Products Ordered: {total}\")\n",
" print(f\"Percentage of Products Ordered: {percentage:.1f}%\")\n",
"\n",
"\n",
"# 6. Define print_updated_inventory\n",
"def print_updated_inventory(inventory):\n",
" \"\"\"Prints the updated inventory.\"\"\"\n",
" print(\"\\nUpdated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"\n",
"# 7. Call the functions in the appropriate sequence to execute the program\n",
"def main():\n",
" # Step 1: Initialize the inventory dictionary\n",
" inventory = initialize_inventory(products)\n",
" \n",
" # Step 2: Get customer orders as a set\n",
" customer_orders = get_customer_orders()\n",
" \n",
" # Step 6 preview: Print the items ordered\n",
" print(\"\\nProducts ordered:\")\n",
" print(customer_orders)\n",
" \n",
" # Step 4: Calculate statistics (returns a tuple containing total and percentage)\n",
" stats = calculate_order_statistics(customer_orders, products)\n",
" \n",
" # Step 5: Print those statistics\n",
" print_order_statistics(stats)\n",
" \n",
" # Step 3: Update the inventory values based on orders\n",
" update_inventory(customer_orders, inventory)\n",
" \n",
" # Step 6: Print the final updated inventory\n",
" print_updated_inventory(inventory)\n",
"\n",
"\n",
"\n",
"main()\n"
]
}
],
"metadata": {
Expand All @@ -61,7 +159,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.6"
}
},
"nbformat": 4,
Expand Down