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
149 changes: 147 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,156 @@
"\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": "3585b1e1",
"metadata": {},
"outputs": [],
"source": [
"#1\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
"\n",
" for product in products:\n",
" valid_input = False\n",
"\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",
"\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid quantity.\")\n",
"\n",
" return inventory\n",
"\n",
"\n",
"inventory = initialize_inventory(products)\n",
"inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "70351c19",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders(inventory):\n",
" valid_number = False\n",
"\n",
" while not valid_number:\n",
" try:\n",
" number_of_orders = int(input(\"How many items would you like to order? \"))\n",
"\n",
" if number_of_orders > 0:\n",
" valid_number = True\n",
" else:\n",
" print(\"Number of orders must be positive. Please try again.\")\n",
"\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid number.\")\n",
"\n",
" customer_orders = set()\n",
"\n",
" for _ in range(number_of_orders):\n",
" valid_product = False\n",
"\n",
" while not valid_product:\n",
" try:\n",
" product = input(\n",
" \"Enter a product name (t-shirt, mug, hat, book, keychain): \"\n",
" ).lower()\n",
"\n",
" if product not in inventory:\n",
" raise ValueError(\"Invalid product name.\")\n",
"\n",
" if inventory[product] <= 0:\n",
" raise ValueError(\"Product is out of stock.\")\n",
"\n",
" customer_orders.add(product)\n",
" valid_product = True\n",
"\n",
" except ValueError as error:\n",
" print(error)\n",
" print(\"Please enter a valid product name with stock available.\")\n",
"\n",
" return customer_orders\n",
"\n",
"\n",
"customer_orders = get_customer_orders(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cabce962",
"metadata": {},
"outputs": [],
"source": [
"def calculate_total_price(customer_orders):\n",
" total_price = 0\n",
" for product in customer_orders:\n",
" valid_price = False\n",
"\n",
" while not valid_price:\n",
" try:\n",
" price = float(input(f\"Enter the price of {product}: \"))\n",
"\n",
" if price >= 0:\n",
" total_price += price\n",
" valid_price = True\n",
" else:\n",
" print(\"Price cannot be negative. Please enter a valid price.\")\n",
"\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a numeric price.\")\n",
"\n",
" return total_price\n",
"\n",
"\n",
"total_price = calculate_total_price(customer_orders)\n",
"print(f\"Total Price: {total_price}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "480c4ba5",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "f69a11f1",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "70ed2e18",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +235,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.7"
}
},
"nbformat": 4,
Expand Down