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
210 changes: 209 additions & 1 deletion lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,214 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "dcb75892-09c5-45d0-991e-9a9db90f2339",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Quantify t-shirt product: 10\n",
"Quantify mug product: 5\n",
"Quantify hat product: 6\n",
"Quantify book product: 7\n",
"Quantify keychain product: 10\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 10, 'mug': 5, 'hat': 6, 'book': 7, 'keychain': 10}\n"
]
}
],
"source": [
"#1 \n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory ={\"t-shirt\": 0, \"mug\": 0, \"hat\": 0, \"book\": 0, \"keychain\": 0}\n",
"\n",
"def initialize_inventory(products):\n",
" for product in inventory:\n",
" inventory[product] = int(input(f\"Quantify {product} product: \"))\n",
"\n",
"initialize_inventory(products)\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "3f4b0369-9c26-4596-bffd-2f84ac5ecba5",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"How many products do you want to order? 2\n",
"Enter product name: book\n",
"Enter product name: t-shirt\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt', 'book'}\n"
]
}
],
"source": [
"#2\n",
"def get_customer_orders():\n",
" n = int(input(\"How many products do you want to order? \"))\n",
" \n",
" customer_orders = set()\n",
" \n",
" for _ in range(n):\n",
" product = input(\"Enter product name: \").strip().lower()\n",
" customer_orders.add(product)\n",
" \n",
" return customer_orders \n",
"\n",
"customer_orders = get_customer_orders()\n",
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "d4cb11ce-2d00-443c-86a7-592e099202a9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 9, 'mug': 5, 'hat': 6, 'book': 6, 'keychain': 10}\n"
]
}
],
"source": [
"#3\n",
"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",
" else:\n",
" print(f\"{product} is out of stock\")\n",
" else:\n",
" print(f\"{product} does not exist in inventory.\")\n",
"\n",
" return inventory\n",
"\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"print(inventory)\n",
"\n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "c00fa9a7-0057-4c95-9454-23d132ee8046",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total products ordered: 2\n",
"Percentage of unique produts ordered: 40.00%\n"
]
}
],
"source": [
"#4\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_unique = (total_products_ordered / len (products)) * 100\n",
"\n",
" return total_products_ordered, percentage_unique \n",
"\n",
"total, percentage = calculate_order_statistics(orders, products)\n",
"\n",
"print(f\"Total products ordered: {total}\")\n",
"print(f\"Percentage of unique produts ordered: {percentage: .2f}%\")"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "9b366de3-afde-4e9a-ad48-9701354f6924",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total products ordered: 2\n",
"Percentage of unique products ordered: 40.00%\n"
]
}
],
"source": [
"def print_order_statistics(order_statistics):\n",
" total, percentage = order_statistics\n",
" \n",
" print(\"Order Statistics:\")\n",
" print(f\"Total products ordered: {total}\")\n",
" print(f\"Percentage of unique products ordered: {percentage:.2f}%\")\n",
"\n",
"order_statistics = calculate_order_statistics(orders, products)\n",
"print_order_statistics(order_statistics)\n"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "1115baa1-dd6b-498b-bfeb-a5c821f7b211",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated Inventory:\n",
"t-shirt: 7\n",
"mug: 5\n",
"hat: 6\n",
"book: 4\n",
"keychain: 10\n"
]
}
],
"source": [
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" \n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"inventory = update_inventory(orders, inventory)\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d885da01-ac71-40bf-a9cf-717c11386202",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -61,7 +269,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down