Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

done #286

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

done #286

Show file tree
Hide file tree
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
264 changes: 264 additions & 0 deletions .ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e",
"metadata": {},
"source": [
"# Lab | Error Handling"
]
},
{
"cell_type": "markdown",
"id": "bc99b386-7508-47a0-bcdb-d969deaf6c8b",
"metadata": {},
"source": [
"## Exercise: Error Handling for Managing Customer Orders\n",
"\n",
"The implementation of your code for managing customer orders assumes that the user will always enter a valid input. \n",
"\n",
"For example, we could modify the `initialize_inventory` function to include error handling.\n",
" - If the user enters an invalid quantity (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the quantity for that product.\n",
" - Use a try-except block to handle the error and continue prompting the user until a valid quantity is entered.\n",
"\n",
"```python\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\n",
"\n",
"# Or, in another way:\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\n",
"```\n",
"\n",
"Let's enhance your code by implementing error handling to handle invalid inputs.\n",
"\n",
"Follow the steps below to complete the exercise:\n",
"\n",
"2. Modify the `calculate_total_price` function to include error handling.\n",
" - If the user enters an invalid price (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the price for that product.\n",
" - Use a try-except block to handle the error and continue prompting the user until a valid price is entered.\n",
"\n",
"3. Modify the `get_customer_orders` function to include error handling.\n",
" - If the user enters an invalid number of orders (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the number of orders.\n",
" - If the user enters an invalid product name (e.g., a product name that is not in the inventory), or that doesn't have stock available, display an error message and ask them to re-enter the product name. *Hint: you will need to pass inventory as a parameter*\n",
" - Use a try-except block to handle the error and continue prompting the user until a valid product name is entered.\n",
"\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": null,
"id": "2108b9c0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the inventory management system!\n",
"\n",
"--- Initializing Inventory ---\n",
"Enter the quantity of apples available: r\n",
"Invalid input. Please enter a valid integer quantity.\n",
"Enter the quantity of apples available: 4\n",
"Enter the quantity of bananas available: 4\n",
"Enter the quantity of oranges available: 4\n",
"\n",
"--- Calculating Total Price ---\n",
"Enter the price for apple: e\n",
"Invalid input. Please enter a valid numeric price.\n",
"Enter the price for apple: e\n",
"Invalid input. Please enter a valid numeric price.\n",
"Enter the price for apple: r\n",
"Invalid input. Please enter a valid numeric price.\n",
"Enter the price for apple: c\n",
"Invalid input. Please enter a valid numeric price.\n",
"Enter the price for apple: g\n",
"Invalid input. Please enter a valid numeric price.\n",
"Enter the price for apple: s\n",
"Invalid input. Please enter a valid numeric price.\n",
"Enter the price for apple: 5\n",
"Enter the price for banana: 5\n",
"Enter the price for orange: 5\n",
"Total price of all products: $60.00\n",
"\n",
"--- Customer Orders ---\n",
"Available products and their quantities:\n",
"apple: 4\n",
"banana: 4\n",
"orange: 4\n",
"Enter the number of different products to order: e\n",
"Invalid input. Please enter a valid integer.\n",
"Enter the number of different products to order: e\n",
"Invalid input. Please enter a valid integer.\n"
]
}
],
"source": [
"#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",
"products = [\"apple\", \"banana\", \"orange\"]\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",
" \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 integer quantity.\")\n",
" \n",
" return inventory\n",
"\n",
"#2. Modify the calculate_total_price function to include error handling.\n",
"\n",
"def calculate_total_price(products, inventory):\n",
" total_price = 0.0\n",
" for product in products:\n",
" valid_input = False\n",
" while not valid_input:\n",
" try:\n",
" price = float(input(f\"Enter the price for {product}: \"))\n",
" \n",
" if price >= 0:\n",
" \n",
" total_price += price * inventory[product]\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 valid numeric price.\")\n",
" \n",
" return total_price\n",
"\n",
"#3.Modify the get_customer_orders function to include error handling.\n",
"\n",
"\n",
"def get_customer_orders(inventory):\n",
" customer_orders = []\n",
" \n",
" # Display available products and their quantities\n",
" print(\"Available products and their quantities:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
" valid_order_number = False\n",
"\n",
" # Prompt for the number of orders\n",
" while not valid_order_number:\n",
" try:\n",
" num_orders = int(input(\"Enter the number of different products to order: \"))\n",
" if num_orders >= 0:\n",
" valid_order_number = True\n",
" else:\n",
" print(\"The number of orders cannot be negative. Please enter a valid number.\")\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid integer.\")\n",
"\n",
" # Prompt for each product order\n",
" for _ in range(num_orders):\n",
" valid_product = False\n",
" while not valid_product:\n",
" product_name = input(\"Enter the product name you want to order: \").strip()\n",
" \n",
" # Check if product is in inventory and has stock available\n",
" if product_name in inventory:\n",
" if inventory[product_name] > 0:\n",
" valid_product = True\n",
" customer_orders.append(product_name) # Add product to orders\n",
" inventory[product_name] -= 1 # Decrement the inventory\n",
" else:\n",
" print(f\"Sorry, {product_name} is out of stock. Please choose a different product.\")\n",
" else:\n",
" print(\"Invalid product name. Please enter a valid product from the inventory.\")\n",
"\n",
" return customer_orders\n",
"\n",
"\n",
"def main():\n",
" print(\"Welcome to the inventory management system!\")\n",
"\n",
" # Step 1: Initialize the inventory\n",
" print(\"\\n--- Initializing Inventory ---\")\n",
" inventory = initialize_inventory(products)\n",
"\n",
" # Step 2: Calculate total price of products\n",
" print(\"\\n--- Calculating Total Price ---\")\n",
" total_price = calculate_total_price(products, inventory)\n",
" print(f\"Total price of all products: ${total_price:.2f}\")\n",
"\n",
" # Step 3: Get customer orders\n",
" print(\"\\n--- Customer Orders ---\")\n",
" orders = get_customer_orders(inventory)\n",
" print(f\"Customer orders: {orders}\")\n",
" print(f\"Remaining inventory: {inventory}\")\n",
"\n",
"# Run the program\n",
"main()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "70d3ac87",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading