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
150 changes: 148 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,157 @@
"\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": 5,
"id": "aef49fe3",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(product):\n",
" inventory = {}\n",
" for product in products:\n",
" valid_input = False\n",
" while not valid_input:\n",
" try:\n",
" quantity = int(input(f\"Enter quantity of {product}: \"))\n",
" if quantity >= 0:\n",
" inventory[product] = quantity\n",
" valid_input = True\n",
" else:\n",
" print(\"Quantity can not be negative. Please enter a valid quantity.\")\n",
" except ValueError:\n",
" print(\"Invalid input, Please enter a valid quantity.\")\n",
"\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "1a10a2c3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input, Please enter a valid quantity.\n",
"Invalid input, Please enter a valid quantity.\n",
"Invalid input, Please enter a valid quantity.\n"
]
}
],
"source": [
"inventory = initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "4dbf62af",
"metadata": {},
"outputs": [],
"source": [
"def calculate_total_order_value(customer_orders):\n",
" for products in customer_orders:\n",
" Valid_input = False\n",
" while not Valid_input:\n",
" try:\n",
" product_prices = float(input(f\"Enter price for {products}: \"))\n",
" total_price = 0\n",
" total_price += product_prices\n",
"\n",
" if product_prices > 0:\n",
" Valid_input = True\n",
" else:\n",
" print(\"Price cannot be 0 or negative. Please enter a valid price.\")\n",
"\n",
" except ValueError:\n",
" print(\"Invalid input, Please enter a valid price.\")\n",
" return total_price"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "fc910856",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_order():\n",
" customer_orders = set()\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 positive. Please enter a valid number.\")\n",
" except ValueError:\n",
" print(\"Invalid input, Please enter a valid number.\")\n",
"\n",
" for _ in range(num_orders):\n",
" while True:\n",
" try:\n",
" product_ordered = input(\"Which product does customer order?\").strip().lower()\n",
" if not product_ordered in products:\n",
" raise ValueError(\"Product not found in inventory.\")\n",
" customer_orders.add(product_ordered)\n",
" break \n",
" except ValueError: \n",
" print(\"Product not found in inventory. Please enter a valid product.\")\n",
" \n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "5dbaa242",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Product not found in inventory. Please enter a valid product.\n"
]
}
],
"source": [
"customer_orders = get_customer_order()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8873ccb4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Price cannot be 0 or negative. Please enter a valid price.\n",
"The total order value is: 90.0\n"
]
}
],
"source": [
"\n",
"total_price = calculate_total_order_value(customer_orders)\n",
"print(f\"The total order value is: {total_price}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +236,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down