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
157 changes: 155 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,164 @@
"\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": 32,
"id": "fafb9721",
"metadata": {},
"outputs": [],
"source": [
"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 in stock: \"))\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": "markdown",
"id": "4fb44703",
"metadata": {},
"source": [
"Answer 2:"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "52914694",
"metadata": {},
"outputs": [],
"source": [
"def calculate_total_price(customer_orders):\n",
" prices = {}\n",
" total_price = 0\n",
"\n",
" for product, quantity in customer_orders.items(): \n",
" while True:\n",
" try:\n",
" price_input = input(f\"Enter the price for {product}: \")\n",
" price = float(price_input)\n",
" if price < 0:\n",
" raise ValueError(\"Price cannot be negative.\")\n",
" prices[product] = price\n",
" break\n",
" except ValueError:\n",
" print(\"Invalid price! Please enter a valid number.\")\n",
" \n",
" total_price += price * quantity\n",
"\n",
" print(f\"Total Order Price: {total_price}\")\n",
" return total_price\n"
]
},
{
"cell_type": "markdown",
"id": "5ac9e438",
"metadata": {},
"source": [
"Answer 3:"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "8ee4a7a1",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders(inventory):\n",
" orders = {}\n",
"\n",
" while True:\n",
" product = input(\"Enter a product name (or 'done' to finish): \").strip()\n",
"\n",
" if product.lower() == 'done':\n",
" break\n",
"\n",
" try:\n",
" if product not in inventory:\n",
" raise KeyError(f\"Product '{product}' not found in inventory.\")\n",
" if inventory[product] <= 0:\n",
" raise ValueError(f\"Product '{product}' is out of stock.\")\n",
"\n",
" while True:\n",
" try:\n",
" quantity_input = input(f\"Enter the quantity of '{product}' ordered: \")\n",
" quantity = int(quantity_input)\n",
" if quantity <= 0:\n",
" raise ValueError(\"Quantity must be a positive number.\")\n",
" if quantity > inventory[product]:\n",
" raise ValueError(f\"Only {inventory[product]} units of '{product}' are available.\")\n",
" orders[product] = quantity\n",
" break \n",
" except ValueError as error:\n",
" print(f\"Error: {error} \")\n",
" \n",
" except (KeyError, ValueError) as error:\n",
" print(f\"Error: {error} \")\n",
" return orders\n"
]
},
{
"cell_type": "markdown",
"id": "71361431",
"metadata": {},
"source": [
"Answer 4:"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "9fc619dd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: invalid literal for int() with base 10: 'two'\n",
"Error: invalid literal for int() with base 10: 'one'\n",
"Error: \"Product 'hatt' not found in inventory.\" \n",
"Error: Only 4 units of 'hat' are available. \n",
"Error: Only 1 units of 'mug' are available. \n",
"Total Order Price: 30.0\n"
]
},
{
"data": {
"text/plain": [
"30.0"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\"]\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders(inventory)\n",
"\n",
"calculate_total_price(customer_orders)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +243,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down