diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..a68a8e1 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,196 @@ "\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": "c169f7e4", + "metadata": {}, + "outputs": [], + "source": [ + "# Cell 1: products list\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "6301ba90", + "metadata": {}, + "outputs": [], + "source": [ + "# Cell 2: initialize_inventory function\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + "\n", + " for product in products:\n", + " valid_input = False\n", + "\n", + " while not valid_input:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + "\n", + " if quantity >= 0:\n", + " inventory[product] = quantity\n", + " valid_input = True\n", + " else:\n", + " print(\"Quantity cannot be negative. Please enter a valid quantity.\")\n", + "\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a whole number.\")\n", + "\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "d87d0756", + "metadata": {}, + "outputs": [], + "source": [ + "# Cell 3: get_customer_orders function\n", + "def get_customer_orders(inventory):\n", + " customer_orders = {}\n", + "\n", + " valid_input = False\n", + "\n", + " while not valid_input:\n", + " try:\n", + " number_of_orders = int(input(\"How many different products would the customer like to order? \"))\n", + "\n", + " if number_of_orders > 0:\n", + " valid_input = True\n", + " else:\n", + " print(\"Number of orders must be greater than zero.\")\n", + "\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a whole number.\")\n", + "\n", + " for i in range(number_of_orders):\n", + " valid_product = False\n", + "\n", + " while not valid_product:\n", + " product_order = input(\"Enter the name of a product: \").lower()\n", + "\n", + " if product_order not in inventory:\n", + " print(\"Error: That product is not in the inventory.\")\n", + "\n", + " elif inventory[product_order] <= 0:\n", + " print(\"Error: That product is out of stock.\")\n", + "\n", + " else:\n", + " valid_quantity = False\n", + "\n", + " while not valid_quantity:\n", + " try:\n", + " order_quantity = int(input(f\"Enter the quantity of {product_order} ordered: \"))\n", + "\n", + " if order_quantity <= 0:\n", + " print(\"Order quantity must be greater than zero.\")\n", + "\n", + " elif order_quantity > inventory[product_order]:\n", + " print(f\"Not enough stock. Only {inventory[product_order]} available.\")\n", + "\n", + " else:\n", + " customer_orders[product_order] = order_quantity\n", + " valid_quantity = True\n", + " valid_product = True\n", + "\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a whole number.\")\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "324c1f58", + "metadata": {}, + "outputs": [], + "source": [ + "# Cell 4: calculate_total_price function\n", + "def calculate_total_price(customer_orders):\n", + " total_price = 0.0\n", + "\n", + " for product, quantity in customer_orders.items():\n", + " valid_input = False\n", + "\n", + " while not valid_input:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + "\n", + " if price >= 0:\n", + " total_price += price * quantity\n", + " valid_input = True\n", + " else:\n", + " print(\"Price cannot be negative. Please enter a valid price.\")\n", + "\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a numeric price.\")\n", + "\n", + " return total_price" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "ab01adc6", + "metadata": {}, + "outputs": [], + "source": [ + "# Cell 5: update_inventory function\n", + "def update_inventory(customer_orders, inventory):\n", + " for product, quantity in customer_orders.items():\n", + " inventory[product] -= quantity\n", + "\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1f005003", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: That product is out of stock.\n", + "Error: That product is out of stock.\n", + "Error: That product is not in the inventory.\n", + "Error: That product is out of stock.\n", + "Error: That product is not in the inventory.\n" + ] + } + ], + "source": [ + "# Cell 6: run the program\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders(inventory)\n", + "total_price = calculate_total_price(customer_orders)\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "\n", + "print(\"Customer orders:\", customer_orders)\n", + "print(\"Total price:\", total_price)\n", + "print(\"Updated inventory:\", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "672ea904", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -90,7 +275,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.10" } }, "nbformat": 4,