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
70 changes: 69 additions & 1 deletion lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,75 @@
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"# your code goes here\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" try:\n",
" for product in products:\n",
" while True:\n",
" try:\n",
" user_quantity = int(input(f\"Introduce the quantity of {product}:\"))\n",
" if user_quantity < 0:\n",
" raise ValueError(\"Quantity cannot be negative. Please enter a valid quantity.\")\n",
" inventory[product] = user_quantity\n",
" break # exit the loop if the input is valid\n",
" except ValueError as e:\n",
" print(f\"Error: {e}\")\n",
" continue # prompt the user for input again\n",
" except Exception as e:\n",
" print(f\"An unexpected error occurred while initializing inventory: {e}\")\n",
" return inventory\n",
"\n",
"def get_customer_orders(products, inventory):\n",
" customer_orders = set()\n",
"\n",
" try:\n",
" while True:\n",
" orders = input('Please enter the name of a product: ').lower()\n",
" if orders in products and inventory.get(orders, 0) > 0:\n",
" customer_orders.add(orders)\n",
" else:\n",
" print(f\"That product doesn't exist or is out of stock. Choose another one from {products}\")\n",
"\n",
" more_orders = input('Would you like to add another product? (Y/N): ').strip().upper()\n",
" if more_orders != 'Y':\n",
" break\n",
" except Exception as e:\n",
" print(f\"An unexpected error occurred while getting customer orders: {e}\")\n",
" return customer_orders\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" try:\n",
" for product in customer_orders:\n",
" if inventory.get(product, 0) > 0:\n",
" inventory[product] -= 1\n",
" else:\n",
" print(f\"There is no stock for {product}.\")\n",
" except KeyError as e:\n",
" print(f\"Error: {e}. The product does not exist in the inventory.\")\n",
" except Exception as e:\n",
" print(f\"An unexpected error occurred while updating the inventory: {e}\")\n",
" return inventory\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" try:\n",
" # Total Products Ordered\n",
" total_products = len(customer_orders)\n",
"\n",
" # Percentage of Products Ordered\n",
" if len(products) > 0: # Avoid division by zero\n",
" percentage_products = (total_products / len(products)) * 100\n",
" else:\n",
" percentage_products = 0 # If no products, set to 0%\n",
"\n",
" order_statistics = total_products, percentage_products\n",
" except ZeroDivisionError:\n",
" print(\"Error: No products available to calculate the percentage.\")\n",
" order_statistics = 0, 0\n",
" except Exception as e:\n",
" print(f\"An unexpected error occurred while calculating order statistics: {e}\")\n",
" order_statistics = 0, 0\n",
" return order_statistics\n"
]
}
],
Expand Down