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: 98 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,107 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a6d16247",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"List of inventory: {'t-shirt': 45, 'mug': 56, 'hat': 76, 'book': 34, 'keychain': 90}\n"
]
}
],
"source": [
"#1. Define a function named `initialize_inventory` that takes `products` as a parameter. \n",
"# 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",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(f\"Enter the quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
" return inventory\n",
"\n",
"inventory = initialize_inventory(products)\n",
"print(\"List of inventory: \", inventory)\n",
"\n",
"#2. Define a function named `get_customer_orders` that takes no parameters. \n",
"# Inside the function, implement the code for prompting the user to enter the product names using a loop. \n",
"# The function should return the `customer_orders` set.\n",
"\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" while True:\n",
" order = input(\"Enter a product name to order: \")\n",
" if order in products:\n",
" customer_orders.add(order)\n",
" print(f\"{order} added to your order.\")\n",
"\n",
" else:\n",
" print(\"Product not available.\")\n",
"\n",
" user_input = input(\"Do you want to add another product? (yes/no): \")\n",
" if user_input != \"yes\":\n",
" print(\"Order completed.\") \n",
" break\n",
" return customer_orders\n",
" \n",
"\n",
"#3. Define a function named `update_inventory` that takes `customer_orders` and `inventory` as parameters. \n",
"# Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n",
"\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" inventory[product] -= 1\n",
" return inventory \n",
"\n",
"\n",
"#4. Define a function named `calculate_order_statistics` that takes `customer_orders` and `products` as parameters. \n",
"# 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_products_ordered = len(customer_orders)\n",
" percentage_of_products_ordered = (total_products_ordered / len(products)) * 100\n",
" return total_products_ordered, percentage_of_products_ordered\n",
"\n",
"\n",
"#5. Define a function named `print_order_statistics` that takes `order_statistics` as a parameter. \n",
"# Inside the function, implement the code for printing the order statistics.\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" total_products_ordered, percentage_of_products_ordered = order_statistics\n",
" print(\"Total Products Ordered:\", total_products_ordered)\n",
" print(f\"Percentage of Products Ordered: {percentage_of_products_ordered}%\")\n",
"\n",
"#6. Define a function named `print_updated_inventory` that takes `inventory` as a parameter. \n",
"# Inside the function, implement the code for printing the updated inventory.\n",
"\n",
"def print_updated_inventory(inventory):\n",
" for product, quantity in inventory.items():\n",
" print(f\"Updated quantity for {product}: {quantity}\")\n",
"\n",
"#7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"\n",
"customer_orders = get_customer_orders()\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(order_statistics)\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"print_updated_inventory(inventory)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +157,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down