diff --git a/lab-python-error-handling-solution.ipynb b/lab-python-error-handling-solution.ipynb new file mode 100644 index 0000000..98587b3 --- /dev/null +++ b/lab-python-error-handling-solution.ipynb @@ -0,0 +1,153 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Lab | Error Handling - Solution" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 1: initialize_inventory with error handling (provided by lab)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\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", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid quantity.\")\n", + " return inventory" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 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", + " for product in customer_orders:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " if price >= 0:\n", + " total_price += price\n", + " valid_input = True\n", + " else:\n", + " print(\"Price cannot be negative. Please enter a valid price.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a numeric price.\")\n", + " return total_price" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 3: get_customer_orders with error handling" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " # Step 3a: Get valid number of orders\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if num_orders > 0:\n", + " valid_input = True\n", + " else:\n", + " print(\"Number of orders must be greater than 0.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid number.\")\n", + "\n", + " # Step 3b: Get valid product names\n", + " customer_orders = set()\n", + " for _ in range(num_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " try:\n", + " product = input(\"Enter the name of a product that a customer wants to order: \")\n", + " if product not in inventory:\n", + " raise ValueError(f\"'{product}' is not in the inventory. Please enter a valid product name.\")\n", + " if inventory[product] == 0:\n", + " raise ValueError(f\"'{product}' is out of stock. Please choose another product.\")\n", + " customer_orders.add(product)\n", + " valid_product = True\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 4: Run the full program to test" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Run the program\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders(inventory)\n", + "total_price = calculate_total_price(customer_orders)\n", + "\n", + "print(\"\\nCustomer Orders:\", customer_orders)\n", + "print(\"Total Price:\", total_price)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "name": "python", + "version": "3.9.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}