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
202 changes: 199 additions & 3 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,209 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "9a744271-268a-4aa7-91f8-4353eeea0d42",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"STARTING ORDER MANAGEMENT\n",
"\n",
"INITIALIZING INVENTORY\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter quantity of t-shirt: 10\n",
"Enter quantity of mug: 32\n",
"Enter quantity of hat: 20\n",
"Enter quantity of book: 20\n",
"Enter quantity of keychain: 20\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"STARTING ORDER PROCESS\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter product name (or type 'done'): hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"'hat' added.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Add another product? (yes/no): book\\\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Entry Complete\n",
"\n",
"UPDATING INVENTORY\n",
"Stock for 'hat' decreased. New: 19\n",
"\n",
"CALCULATING STATISTICS\n",
"\n",
"ORDER STATISTICS REPORT\n",
"Total unique products ordered: 1\n",
"Percentage of unique products ordered: 20.00%\n",
"\n",
"FINAL INVENTORY REPORT\n",
"Product | Stock\n",
"-------------\n",
"t-shirt | 10\n",
"mug | 32\n",
"hat | 19\n",
"book | 20\n",
"keychain | 20\n",
"\n",
"PROGRAM FINISHED\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" print(\"\\nINITIALIZING INVENTORY\")\n",
" for product in products:\n",
" while True:\n",
" try:\n",
" inventory_volume = input(f\"Enter quantity of {product}: \")\n",
" inventory_volume = int(inventory_volume)\n",
" if inventory_volume < 0:\n",
" print(\"Quantity must be positive.\")\n",
" continue\n",
" break\n",
" except ValueError:\n",
" print(\"Invalid input. Enter a whole number.\")\n",
" \n",
" inventory[product] = inventory_volume\n",
" return inventory\n",
"\n",
"def get_customer_orders(inventory):\n",
" customer_orders = set()\n",
" print(\"\\nSTARTING ORDER PROCESS\")\n",
"\n",
" while True:\n",
" product_name = input(\"Enter product name (or type 'done'): \").strip()\n",
" \n",
" if product_name.lower() == 'done':\n",
" break\n",
" \n",
" if product_name in inventory:\n",
" customer_orders.add(product_name)\n",
" print(f\"'{product_name}' added.\")\n",
" else:\n",
" print(f\"'{product_name}' not found. Try again.\")\n",
" continue \n",
"\n",
" add_another = input(\"Add another product? (yes/no): \").strip().lower()\n",
"\n",
" if add_another != 'yes':\n",
" break \n",
" \n",
" print(\"Order Entry Complete\")\n",
" return customer_orders\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" print(\"\\nUPDATING INVENTORY\")\n",
" \n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" if inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" print(f\"Stock for '{product}' decreased. New: {inventory[product]}\")\n",
" else:\n",
" print(f\"'{product}' is out of stock.\")\n",
" \n",
" return inventory\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" print(\"\\nCALCULATING STATISTICS\")\n",
" \n",
" total_products_ordered = len(customer_orders)\n",
" total_unique_products = len(products) \n",
" \n",
" if total_unique_products > 0:\n",
" percentage_ordered = (total_products_ordered / total_unique_products) * 100\n",
" else:\n",
" percentage_ordered = 0.0\n",
" \n",
" return total_products_ordered, percentage_ordered\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" total_products, percentage = order_statistics\n",
" print(\"\\nORDER STATISTICS REPORT\")\n",
" print(f\"Total unique products ordered: {total_products}\")\n",
" print(f\"Percentage of unique products ordered: {percentage:.2f}%\")\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(\"\\nFINAL INVENTORY REPORT\")\n",
" \n",
" print(\"Product | Stock\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product:<7} | {quantity}\")\n",
"\n",
"def main():\n",
" print(\"STARTING ORDER MANAGEMENT\")\n",
" \n",
" current_inventory = initialize_inventory(products)\n",
" \n",
" orders_set = get_customer_orders(current_inventory)\n",
" \n",
" final_inventory = update_inventory(orders_set, current_inventory)\n",
" stats_tuple = calculate_order_statistics(orders_set, products)\n",
" \n",
" print_order_statistics(stats_tuple)\n",
" \n",
" print_updated_inventory(final_inventory)\n",
"\n",
" print(\"\\nPROGRAM FINISHED\")\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1f77e356-d989-48c6-93ce-35577498aad5",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -61,7 +257,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down