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
135 changes: 133 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,142 @@
"\n",
"4. Test your code by running the program and deliberately entering invalid quantities and product names. Make sure the error handling mechanism works as expected.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "7757d5de",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input. Please enter a valid quantity for {product}.\n",
"Quantity cannot be negative. Please enter a valid quantity.\n",
"Invalid input. Please enter a valid quantity for {product}.\n",
"Product not found in inventory. Please enter a valid product name.\n",
"Product not found in inventory. Please enter a valid product name.\n",
"\n",
"Order Statistics:\n",
"Total products ordered: 4\n",
"Percentage of unique products ordered: 100.00%\n",
"\n",
"Updated Inventory:\n",
"T-shirt: 4 units left\n",
"Pants: 4 units left\n",
"Shoes: 4 units left\n",
"Jacket: 4 units left\n",
"Invalid input. Please enter a valid price for {product}.\n",
"\n",
"Total price for the customer's order: 155.00€\n"
]
}
],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
"\n",
" for product in products:\n",
" while True:\n",
" try:\n",
" quantity = int(input(f\"Enter the quantity for {product}: \"))\n",
" if quantity < 0:\n",
" print(\"Quantity cannot be negative. Please enter a valid quantity.\")\n",
" continue\n",
" inventory[product] = quantity\n",
" break\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid quantity for {product}.\")\n",
" return inventory\n",
"\n",
"def get_costumers_orders():\n",
" while True:\n",
" try: \n",
" num_order = int(input(\"Enter the number of products in the customer's order: \"))\n",
" if num_order <= 0:\n",
" print(\"Number of products must be a positive integer. Please try again.\")\n",
" continue\n",
" break\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid number of products.\")\n",
" \n",
"\n",
" costumers_order = set()\n",
" for _ in range(num_order):\n",
" while True:\n",
" product_name = input(f\"Enter the name of the product that you want to order: \").strip().lower()\n",
" if product_name not in inventory:\n",
" print(\"Product not found in inventory. Please enter a valid product name.\")\n",
" elif inventory[product_name] <= 0:\n",
" print(f\"Sorry, {product_name} is out of stock. Please choose another product.\")\n",
" else:\n",
" costumers_order.add(product_name)\n",
" break\n",
" return costumers_order\n",
"\n",
"def calculate_total_price(costumers_order):\n",
" total_price = 0.0\n",
" for product in costumers_order:\n",
" while True:\n",
" try:\n",
" price = float(input(f\"Enter the price for {product}: \"))\n",
" if price < 0:\n",
" print(\"Price cannot be negative. Please enter a valid price.\")\n",
" continue\n",
" total_price += price\n",
"\n",
" break\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid price for {product}.\")\n",
" continue\n",
" return total_price\n",
"\n",
"def print_total_price(total_price):\n",
" print(f\"\\nTotal price for the customer's order: {total_price:.2f}€\")\n",
"\n",
"\n",
"def update_inventory(costumers_order, inventory):\n",
" inventory = {product: (quantity - 1 if product in costumers_order else quantity) for product, quantity in inventory.items() if (quantity -1 if product in costumers_order else quantity) > 0}\n",
" return inventory\n",
"\n",
"def calculate_order_statistics(costumers_order, products):\n",
" total_products_ordered = len(costumers_order)\n",
" percentage = (total_products_ordered / len(products)) * 100\n",
" return total_products_ordered, percentage\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" total_products_ordered, percentage = order_statistics\n",
" \n",
" print(\"\\nOrder Statistics:\")\n",
" print(f\"Total products ordered: {total_products_ordered}\")\n",
" print(f\"Percentage of unique products ordered: {percentage:.2f}%\")\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(\"\\nUpdated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product.capitalize()}: {quantity} units left\")\n",
"\n",
"products = [\"t-shirt\", \"pants\", \"shoes\", \"jacket\"]\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"costumers_order = get_costumers_orders()\n",
"\n",
"order_statistics = calculate_order_statistics(costumers_order, products)\n",
"print_order_statistics(order_statistics)\n",
"\n",
"inventory = update_inventory(costumers_order, inventory)\n",
"print_updated_inventory(inventory)\n",
"\n",
"valor_final = calculate_total_price(costumers_order)\n",
"print_total_price(valor_final) \n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": ".venv",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +221,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.3"
}
},
"nbformat": 4,
Expand Down