diff --git a/lab-python-error-handling_completed_AZ.ipynb b/lab-python-error-handling_completed_AZ.ipynb new file mode 100644 index 0000000..df82353 --- /dev/null +++ b/lab-python-error-handling_completed_AZ.ipynb @@ -0,0 +1,174 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Lab | Error Handling\n", + "\n", + "Completed version based on the customer-order functions from the previous labs." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": ["## 1. Initialize inventory with error handling"] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "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 of {product}s available: \"))\n", + " if quantity < 0:\n", + " raise ValueError(\"Quantity cannot be negative.\")\n", + " inventory[product] = quantity\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Invalid input: {error} Please enter a non-negative whole number.\")\n", + "\n", + " return inventory" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": ["## 2. Calculate total price with error handling"] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " total_price = 0\n", + "\n", + " for product in customer_orders:\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " if price < 0:\n", + " raise ValueError(\"Price cannot be negative.\")\n", + " total_price += price\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Invalid input: {error} Please enter a non-negative number.\")\n", + "\n", + " return total_price" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": ["## 3. Get customer orders with error handling"] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if num_orders < 0:\n", + " raise ValueError(\"The number of orders cannot be negative.\")\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Invalid input: {error} Please enter a non-negative whole number.\")\n", + "\n", + " customer_orders = []\n", + "\n", + " for _ in range(num_orders):\n", + " while True:\n", + " try:\n", + " product = input(\"Enter the product name: \").strip().lower()\n", + " if product not in inventory:\n", + " raise ValueError(\"This product is not in the inventory.\")\n", + " if inventory[product] <= 0:\n", + " raise ValueError(\"This product is out of stock.\")\n", + " if customer_orders.count(product) >= inventory[product]:\n", + " raise ValueError(\"There is not enough stock for another order of this product.\")\n", + " customer_orders.append(product)\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Invalid product: {error} Available products: \"\n", + " f\"{[name for name, stock in inventory.items() if stock > 0]}\")\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": ["## Supporting functions and complete program"] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(inventory, customer_orders):\n", + " for product in customer_orders:\n", + " inventory[product] -= 1\n", + " return {product: quantity for product, quantity in inventory.items() if quantity > 0}\n", + "\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n", + "\n", + "\n", + "def main():\n", + " products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + " inventory = initialize_inventory(products)\n", + " customer_orders = get_customer_orders(inventory)\n", + " total_price = calculate_total_price(customer_orders)\n", + " inventory = update_inventory(inventory, customer_orders)\n", + "\n", + " print(f\"\\nTotal products ordered: {len(customer_orders)}\")\n", + " print(f\"Total price: {total_price:.2f}\")\n", + " print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 4. Manual test\n", + "\n", + "Run the cell below and deliberately try:\n", + "- text and a negative number for inventory quantity;\n", + "- text and a negative number for the number of orders;\n", + "- an unknown product, an out-of-stock product and more orders than available stock;\n", + "- text and a negative price.\n", + "\n", + "The program should display an error and ask again instead of stopping." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": ["main()"] + } + ], + "metadata": { + "kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, + "language_info": {"name": "python", "version": "3"} + }, + "nbformat": 4, + "nbformat_minor": 5 +}