Skip to content
Open
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
101 changes: 98 additions & 3 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,108 @@
" - 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"
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e8160c53",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: Quantity cannot be negative.. Please enter a valid quantity\n",
"Error: invalid literal for int() with base 10: 'e'. Please enter a valid quantity\n",
"Invalid input. Please re-enter the price.\n",
"Invalid input. Please re-enter the price.\n",
"Error: Number of orders cannot be negative.. Please enter a valid number\n",
"Error: invalid literal for int() with base 10: 'f'. Please enter a valid number\n",
"Error: 'chair' not found in inventory.\n",
"Error: 'bool' not found in inventory.\n",
"Error: Product out of stock.\n",
"['hat', 'keychain']\n"
]
}
],
"source": [
"# 1 \n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\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 quantity for {product}: \"))\n",
" if quantity < 0:\n",
" raise ValueError(\"Quantity cannot be negative.\")\n",
" inventory[product] = quantity\n",
" break\n",
" except ValueError as e:\n",
" print(f\"Error: {e}. Please enter a valid quantity\")\n",
"\n",
" return inventory\n",
"\n",
"#2\n",
"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 e:\n",
" print(f\"Error: {e}. Please re-enter the price.\")\n",
"\n",
" return total_price\n",
"\n",
"#3\n",
"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(\"Number of orders cannot be negative.\")\n",
" break\n",
" except ValueError as e:\n",
" print(f\"Error: {e}. Please enter a valid number\")\n",
"\n",
" customer_orders = []\n",
" for _ in range(num_orders):\n",
" while True:\n",
" try:\n",
" product = input(\"Enter product name: \").strip().lower()\n",
" if product not in inventory:\n",
" raise ValueError(f\"'{product}' not found in inventory.\")\n",
" if inventory[product] == 0:\n",
" raise ValueError(\"Product out of stock.\")\n",
"\n",
" customer_orders.append(product)\n",
" break\n",
"\n",
" except ValueError as e:\n",
" print(f\"Error: {e}\")\n",
"\n",
" return customer_orders\n",
"\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders(inventory)\n",
"total_price = calculate_total_price (customer_orders)\n",
"print(customer_orders)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +185,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.3"
}
},
"nbformat": 4,
Expand Down