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
216 changes: 214 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,223 @@
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "409adc14",
"metadata": {},
"source": [
"Answear 1:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "ccd6f2dd",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(f\"Enter quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
" return inventory"
]
},
{
"cell_type": "markdown",
"id": "72072bb0",
"metadata": {},
"source": [
"Answear 2:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "efea27c5",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" customer_orders = set()\n",
" while True:\n",
" order = input(\"Enter a product to order (or type 'done' to finish): \")\n",
" if order.lower() == 'done':\n",
" break\n",
" customer_orders.add(order)\n",
" return customer_orders"
]
},
{
"cell_type": "markdown",
"id": "976ede21",
"metadata": {},
"source": [
"Answear 3:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "27592d16",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" if inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" print(f\"{product} order processed. Remaining: {inventory[product]}\")\n",
" else:\n",
" print(f\"Sorry, {product} is out of stock.\")\n",
" else:\n",
" print(f\"{product} not available.\")\n",
"\n",
" return inventory\n"
]
},
{
"cell_type": "markdown",
"id": "07260a35",
"metadata": {},
"source": [
"Answear 4:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "6122775e",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" total_ordered = len(customer_orders)\n",
" total_products = len(products)\n",
"\n",
" if total_products > 0:\n",
" percentage_unique = (total_ordered / total_products) * 100\n",
" else:\n",
" percentage_unique = 0\n",
"\n",
" return total_ordered, percentage_unique\n"
]
},
{
"cell_type": "markdown",
"id": "001b7633",
"metadata": {},
"source": [
"Answear 5:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "8fd78209",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_stats):\n",
" total_ordered, percentage_unique = order_stats\n",
" print(f\"Total unique products ordered: {total_ordered}\")\n",
" print(f\"Percentage of unique products ordered: {percentage_unique:.2f}%\")"
]
},
{
"cell_type": "markdown",
"id": "88a97ae9",
"metadata": {},
"source": [
"Answear 6:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "3b29cd4d",
"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": "markdown",
"id": "a09f5ae4",
"metadata": {},
"source": [
"Answear 7:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "d45a57ab",
"metadata": {},
"outputs": [],
"source": [
"def program():\n",
" products = [\"pens\", \"keychains\", \"t-shirts\", \"books\"]\n",
"\n",
" print(\"Initializing inventory...\")\n",
" inventory = initialize_inventory(products)\n",
"\n",
" print(\"\\nGetting customer orders...\")\n",
" customer_orders = get_customer_orders()\n",
"\n",
" print(\"\\nUpdating inventory...\")\n",
" inventory = update_inventory(customer_orders, inventory)\n",
"\n",
" order_stats = calculate_order_statistics(customer_orders, products)\n",
" print_order_statistics(order_stats)\n",
"\n",
" print_updated_inventory(inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "49ff33e3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initializing inventory...\n",
"\n",
"Getting customer orders...\n",
"\n",
"Updating inventory...\n",
"t-shirts order processed. Remaining: 4\n",
"keychains order processed. Remaining: 5\n",
"books order processed. Remaining: 3\n",
"pens order processed. Remaining: 4\n",
"Total unique products ordered: 4\n",
"Percentage of unique products ordered: 100.00%\n",
"Updated Inventory:\n",
"pens: 4\n",
"keychains: 5\n",
"t-shirts: 4\n",
"books: 3\n"
]
}
],
"source": [
"program()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +273,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down