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
127 changes: 124 additions & 3 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,134 @@
" - 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.\n",
"het"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4e9b7c55",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: invalid literal for int() with base 10: '' Please enter a valid non-negative integer.\n",
"Error: invalid literal for int() with base 10: '' Please enter a valid non-negative integer.\n",
"\n",
"Inventory initialized: {'t-shirt': 2, 'mug': 3, 'hat': 3, 'book': 4, 'keychain': 43}\n",
"\n",
"Total inventory value: 745.00\n",
"Error: Product does not exist in inventory. Please enter a valid product name.\n",
"Error: Product does not exist in inventory. Please enter a valid product name.\n"
]
}
],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
"\n",
" for product in products:\n",
" while True:\n",
" try:\n",
" quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n",
" if quantity < 0:\n",
" raise ValueError(\"Quantity cannot be negative.\")\n",
" inventory[product] = quantity\n",
" break\n",
" except ValueError as error:\n",
" print(f\"Error: {error} Please enter a valid non-negative integer.\")\n",
"\n",
" return inventory\n",
"\n",
"\n",
"def calculate_total_price(inventory):\n",
" total_price = 0\n",
"\n",
" for product, quantity in inventory.items():\n",
" while True:\n",
" try:\n",
" price = float(input(f\"Enter the price of one {product}: \"))\n",
" if price < 0:\n",
" raise ValueError(\"Price cannot be negative.\")\n",
" total_price += quantity * price\n",
" break\n",
" except ValueError as error:\n",
" print(f\"Error: {error} Please enter a valid non-negative number.\")\n",
"\n",
" return total_price\n",
"\n",
"\n",
"def get_customer_orders(inventory):\n",
" customer_orders = []\n",
"\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 error:\n",
" print(f\"Error: {error} Please enter a valid non-negative integer.\")\n",
"\n",
" for i in range(num_orders):\n",
" while True:\n",
" product = input(f\"Enter product for order {i + 1}: \").strip().lower()\n",
"\n",
" if product not in inventory:\n",
" print(\"Error: Product does not exist in inventory. Please enter a valid product name.\")\n",
" elif inventory[product] <= 0:\n",
" print(\"Error: Product is out of stock. Please choose another product.\")\n",
" else:\n",
" customer_orders.append(product)\n",
" inventory[product] -= 1\n",
" break\n",
"\n",
" return customer_orders\n",
"\n",
"\n",
"def main():\n",
" products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
" inventory = initialize_inventory(products)\n",
" print(\"\\nInventory initialized:\", inventory)\n",
"\n",
" total_price = calculate_total_price(inventory)\n",
" print(f\"\\nTotal inventory value: {total_price:.2f}\")\n",
"\n",
" customer_orders = get_customer_orders(inventory)\n",
" print(\"\\nCustomer orders:\", customer_orders)\n",
" print(\"Updated inventory:\", inventory)\n",
"\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e8ee2f48",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "6e89b5db",
"metadata": {},
"outputs": [],
"source": [
"´"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +211,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down