Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BC_lab #312

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
228 changes: 228 additions & 0 deletions Lab Python Functions.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"id": "e14eafed",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" while True:\n",
" try:\n",
" quantity = input(f\"Enter the quantity for {product}: \").strip()\n",
" if not quantity: \n",
" raise ValueError(\"Input cannot be empty.\")\n",
" quantity = int(quantity) \n",
" if quantity < 0: \n",
" raise ValueError(\"Quantity cannot be negative.\")\n",
" inventory[product] = quantity\n",
" break\n",
" except ValueError as e:\n",
" print(f\"Invalid input: {e}. Please enter a valid number.\")\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "689be0c7",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" customer_orders = {}\n",
" print(\"Enter customer orders. Type 'done' to finish.\")\n",
" while True:\n",
" product = input(\"Enter product name (or 'done' to finish): \").strip()\n",
" if product.lower() == 'done':\n",
" break\n",
" try:\n",
" quantity = input(f\"Enter quantity for {product}: \").strip()\n",
" if not quantity: \n",
" raise ValueError(\"Input cannot be empty.\")\n",
" quantity = int(quantity) \n",
" if quantity < 0: \n",
" raise ValueError(\"Quantity cannot be negative.\")\n",
" customer_orders[product] = customer_orders.get(product, 0) + quantity\n",
" except ValueError as e:\n",
" print(f\"Invalid input: {e}. Please enter a valid number.\")\n",
" return customer_orders\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "a1a7a39d",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" for product, quantity in customer_orders.items():\n",
" if product in inventory:\n",
" inventory[product] -= quantity\n",
" if inventory[product] < 0:\n",
" print(f\"Warning: Not enough {product} in stock. Setting to 0.\")\n",
" inventory[product] = 0\n",
" else:\n",
" print(f\"Warning: {product} is not in inventory.\")\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "ada60701",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = sum(customer_orders.values())\n",
" unique_products_ordered = len([product for product in customer_orders if product in products])\n",
" total_products = len(products)\n",
" percentage_unique_ordered = (unique_products_ordered / total_products) * 100 if total_products > 0 else 0\n",
" return total_products_ordered, percentage_unique_ordered"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "3958f74c",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" total_products_ordered, percentage_unique_ordered = order_statistics\n",
" print(f\"Total Products Ordered: {total_products_ordered}\")\n",
" print(f\"Percentage of Unique Products Ordered: {percentage_unique_ordered:.2f}%\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "0d962c75",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f80e484a",
"metadata": {},
"outputs": [],
"source": [
"if __name__ == \"__main__\":\n",
" products_list = [\"Apple\", \"Banana\", \"Orange\"] \n",
" inventory = initialize_inventory(products_list)\n",
" print(\"Initialized Inventory:\", inventory)\n",
"\n",
" customer_orders = get_customer_orders()\n",
" print(\"Customer Orders:\", customer_orders)\n",
"\n",
" inventory = update_inventory(customer_orders, inventory)\n",
" order_statistics = calculate_order_statistics(customer_orders, products_list)\n",
" \n",
" print_order_statistics(order_statistics)\n",
" print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "29ddb4c6",
"metadata": {},
"outputs": [],
"source": [
"customer_orders = {\"Apple\": 5, \"Banana\": 2, \"Orange\": 7}"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "0f4a1686",
"metadata": {},
"outputs": [],
"source": [
"products_list.sort() \n",
"filtered_products = [p for p in products_list if p.startswith(\"A\")]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "ec9dfa10",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Revenue: 25.0\n"
]
}
],
"source": [
"prices = {\"Apple\": 2.5, \"Banana\": 1.0, \"Orange\": 1.5}\n",
"total_revenue = sum(customer_orders[product] * prices.get(product, 0) for product in customer_orders)\n",
"print(\"Total Revenue:\", total_revenue)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "2847f2b7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'product': 'Apple', 'quantity': 3}, {'product': 'Banana', 'quantity': 10}, {'product': 'Orange', 'quantity': 4}]\n"
]
}
],
"source": [
"bulk_orders = [\n",
" {\"product\": \"Apple\", \"quantity\": 3},\n",
" {\"product\": \"Banana\", \"quantity\": 10},\n",
" {\"product\": \"Orange\", \"quantity\": 4},\n",
"]\n",
"\n",
"for order in bulk_orders:\n",
" product, quantity = order[\"product\"], order[\"quantity\"]\n",
" customer_orders[product] = customer_orders.get(product, 0) + quantity\n",
"\n",
"print(bulk_orders)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}