diff --git a/Lab5Error.ipynb b/Lab5Error.ipynb new file mode 100644 index 0000000..1b48ebc --- /dev/null +++ b/Lab5Error.ipynb @@ -0,0 +1,475 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 6, + "id": "daabb006", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Unique Products Ordered: 40.0\n", + "\n", + "Updated Inventory:\n", + "t-shirt : 3\n", + "mug : 3\n", + "hat : 2\n", + "book : 2\n", + "keychain : 3\n", + "Total Price: 34.900000000000006\n" + ] + } + ], + "source": [ + "# -----------------------\n", + "# Code from Other lab to help with this one\n", + "# -----------------------\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "# ---------\n", + "# Review your code from the previous exercise and identify areas where you can apply comprehension to simplify and streamline your code.\n", + "# ---------\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {\n", + " product: int(input(f\"Enter the quantity of {product}s available: \"))\n", + " for product in products\n", + " }\n", + " return inventory\n", + "\n", + "# ---------\n", + "#Modify the function get_customer_orders so it prompts the user to enter the number of customer orders and gathers the product names using a loop and user input. Use comprehension.\n", + "# --------\n", + "\n", + "def get_customer_orders():\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " customer_orders = [\n", + " input(\"Enter the name of a product that a customer wants to order: \")\n", + " for _ in range(num_orders)\n", + " ]\n", + " return customer_orders\n", + "# -------\n", + "# Add new function & modify the update_inventory\n", + "# -------\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + "\n", + " inventory = {\n", + " product: quantity\n", + " for product, quantity in inventory.items()\n", + " if quantity > 0\n", + " }\n", + "\n", + " return inventory\n", + "\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_unique = (len(set(customer_orders)) / len(products)) * 100\n", + " return total_products_ordered, percentage_unique\n", + "\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " prices = {\n", + " product: float(input(f\"Enter the price of {product}: \"))\n", + " for product in set(customer_orders)\n", + " }\n", + "\n", + " total_price = sum(prices[product] for product in customer_orders)\n", + " return total_price\n", + "\n", + "\n", + "# -------\n", + "# Print\n", + "# -------\n", + "def print_order_statistics(order_statistics):\n", + " total, percentage = order_statistics\n", + " print(\"\\nOrder Statistics:\")\n", + " print(\"Total Products Ordered:\", total)\n", + " print(\"Percentage of Unique Products Ordered:\", percentage)\n", + "\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(product, \":\", quantity)\n", + "\n", + "\n", + "# Program execution (must be outside functions)\n", + "\n", + "inventory = initialize_inventory(products)\n", + "\n", + "customer_orders = get_customer_orders()\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "\n", + "print_order_statistics(order_statistics)\n", + "\n", + "print_updated_inventory(inventory)\n", + "\n", + "total_price = calculate_total_price(customer_orders)\n", + "\n", + "print(\"Total Price:\", total_price)" + ] + }, + { + "cell_type": "markdown", + "id": "e48b2805", + "metadata": {}, + "source": [ + "Lab | Error Handling\n", + "Exercise: Error Handling for Managing Customer Orders" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f4ea5952", + "metadata": {}, + "outputs": [], + "source": [ + "# ---\n", + "# Use a try-except block to handle the error and continue prompting the user until a valid quantity is entered.\n", + "# ---\n", + "\n", + "# Step 1: Define the function for initializing the inventory with error handling\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " valid_quantity = False\n", + " while not valid_quantity:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if quantity < 0:\n", + " raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n", + " valid_quantity = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " inventory[product] = quantity\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "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": "code", + "execution_count": null, + "id": "6251897c", + "metadata": {}, + "outputs": [], + "source": [ + "# ---\n", + "# Modify the calculate_total_price function to include error handling.\n", + "# ---\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " prices = {}\n", + "\n", + " for product in set(customer_orders):\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + "\n", + " if price < 0:\n", + " print(\"Error: Price cannot be in the negative. Please enter a valid price.\")\n", + " continue\n", + "\n", + " prices[product] = price\n", + " break\n", + "\n", + " except ValueError:\n", + " print(\"Error: Invalid input. Please ONLY enter a numeric value.\")\n", + "\n", + " total_price = sum(prices[product] for product in customer_orders)\n", + " return total_price" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "# ---\n", + "# Modify the get_customer_orders function to include error handling.\n", + "# ---\n", + "\n", + "def get_customer_orders(inventory):\n", + "\n", + " # Validate number of orders\n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + "\n", + " if num_orders < 0:\n", + " print(\"Error: Number of orders cannot be negative. Please try again\")\n", + " continue\n", + "\n", + " break\n", + "\n", + " except ValueError:\n", + " print(\"Error: Please enter a valid number.\")\n", + "\n", + " customer_orders = []\n", + "\n", + " # Collect valid product orders\n", + " for _ in range(num_orders):\n", + "\n", + " while True:\n", + " product = input(\"Enter the name of a product that the customer wants to order: \")\n", + "\n", + " if product not in inventory:\n", + " print(\"Error: Product not found in inventory. Try again....\")\n", + " continue\n", + "\n", + " if inventory[product] <= 0:\n", + " print(\"Error: This product is out of stock. Choose another product.\")\n", + " continue\n", + "\n", + " customer_orders.append(product)\n", + " break\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a7e1324a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Please enter a valid number.\n", + "Error: Please enter a valid number.\n", + "Error: This product is out of stock. Choose another product.\n", + "\n", + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Unique Products Ordered: 40.0\n", + "\n", + "Updated Inventory:\n", + "t-shirt : 1\n", + "mug : 1\n", + "keychain : 1\n", + "Error: Invalid input. Please enter a numeric value.\n", + "Total Price: 564.0\n" + ] + } + ], + "source": [ + "# ---\n", + "# Test code to break it\n", + "# ---\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "# -----------------------\n", + "# Initialize inventory\n", + "# -----------------------\n", + "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", + "\n", + " if quantity < 0:\n", + " print(\"Error: Quantity cannot be negative. Please enter a valid quantity.\")\n", + " continue\n", + "\n", + " inventory[product] = quantity\n", + " break\n", + "\n", + " except ValueError:\n", + " print(\"Error: Invalid input. Please enter a whole number.\")\n", + "\n", + " return inventory\n", + "\n", + "\n", + "# -----------------------\n", + "# Get customer orders with error handling\n", + "# -----------------------\n", + "def get_customer_orders(inventory):\n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + "\n", + " if num_orders < 0:\n", + " print(\"Error: Number of orders cannot be negative.\")\n", + " continue\n", + "\n", + " break\n", + "\n", + " except ValueError:\n", + " print(\"Error: Please enter a valid number.\")\n", + "\n", + " customer_orders = []\n", + "\n", + " for _ in range(num_orders):\n", + " while True:\n", + " product = input(\"Enter the name of a product that a customer wants to order: \").lower()\n", + "\n", + " if product not in inventory:\n", + " print(\"Error: Product not found in inventory. Try again.\")\n", + " continue\n", + "\n", + " if inventory[product] <= 0:\n", + " print(\"Error: This product is out of stock. Choose another product.\")\n", + " continue\n", + "\n", + " customer_orders.append(product)\n", + " inventory[product] -= 1\n", + " break\n", + "\n", + " return customer_orders\n", + "\n", + "\n", + "# -----------------------\n", + "# Update inventory\n", + "# -----------------------\n", + "def update_inventory(customer_orders, inventory):\n", + " inventory = {\n", + " product: quantity\n", + " for product, quantity in inventory.items()\n", + " if quantity > 0\n", + " }\n", + " return inventory\n", + "\n", + "\n", + "# -----------------------\n", + "# Order statistics\n", + "# -----------------------\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_unique = (len(set(customer_orders)) / len(products)) * 100\n", + " return total_products_ordered, percentage_unique\n", + "\n", + "\n", + "# -----------------------\n", + "# Calculate total price with error handling\n", + "# -----------------------\n", + "def calculate_total_price(customer_orders):\n", + " prices = {}\n", + "\n", + " for product in set(customer_orders):\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + "\n", + " if price < 0:\n", + " print(\"Error: Price cannot be negative. Please enter a valid price.\")\n", + " continue\n", + "\n", + " prices[product] = price\n", + " break\n", + "\n", + " except ValueError:\n", + " print(\"Error: Invalid input. Please enter a numeric value.\")\n", + "\n", + " total_price = sum(prices[product] for product in customer_orders)\n", + " return total_price\n", + "\n", + "\n", + "# -----------------------\n", + "# Print functions\n", + "# -----------------------\n", + "def print_order_statistics(order_statistics):\n", + " total, percentage = order_statistics\n", + " print(\"\\nOrder Statistics:\")\n", + " print(\"Total Products Ordered:\", total)\n", + " print(\"Percentage of Unique Products Ordered:\", percentage)\n", + "\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(product, \":\", quantity)\n", + "\n", + "\n", + "# -----------------------\n", + "# Program execution\n", + "# -----------------------\n", + "inventory = initialize_inventory(products)\n", + "\n", + "customer_orders = get_customer_orders(inventory)\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "\n", + "print_order_statistics(order_statistics)\n", + "\n", + "print_updated_inventory(inventory)\n", + "\n", + "total_price = calculate_total_price(customer_orders)\n", + "\n", + "print(\"Total Price:\", total_price)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "283ee9b5", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}