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
189 changes: 186 additions & 3 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,196 @@
"\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": 28,
"id": "12951cad-1c3c-41b7-98e0-ac49c70a7fa8",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" valid_input = False\n",
" while not valid_input:\n",
" try:\n",
" quantity = int(input(f\"Enter the quantity of {product}s available: \"))\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",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid quantity.\")\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "11fde404-b794-462b-a828-cbd31958806a",
"metadata": {},
"outputs": [],
"source": [
"def total_price(customer_orders):\n",
" prices = {} \n",
"\n",
" for product in customer_orders:\n",
" if isinstance(product, list):\n",
" product = product[0]\n",
" \n",
" valid_input = False\n",
" while not valid_input:\n",
" try:\n",
" price = float(input(f\"Enter the price of {products}: \"))\n",
" if price >= 0:\n",
" prices[product] = price\n",
" valid_input = True\n",
" else:\n",
" print(\"Price cannot be negative. Please enter a valid price.\")\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a numeric value for the price.\")\n",
"\n",
" total = sum(prices.values())\n",
" return total"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "3e36bb12-94e4-4931-93e5-35210db204a0",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders(inventory):\n",
" \n",
" while True:\n",
" try:\n",
" user_order = int(input(\"Enter the number of customer orders: \"))\n",
" if user_order > 0:\n",
" break\n",
" else:\n",
" print(\"Number of orders must be positive. Please try again.\")\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a numeric value.\")\n",
"\n",
" customer_orders = []\n",
"\n",
" for i in range(user_order):\n",
" while True: \n",
" order = input(f\"Enter the name of a product that a customer wants to order: \").strip()\n",
"\n",
" if order not in inventory:\n",
" print(\"Product not found. Please enter a valid product.\")\n",
" elif inventory[order] <= 0:\n",
" print(f\"Sorry, {order} is out of stock. Please choose another product.\")\n",
" else:\n",
" customer_orders.append(order)\n",
" inventory[order] -= 1 # reducir stock\n",
" break \n",
"\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "842145f9-7ea0-44b7-a370-65eac47ee5c4",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: 4\n",
"Enter the quantity of mugs available: 4\n",
"Enter the quantity of hats available: 4\n",
"Enter the quantity of books available: 4\n",
"Enter the quantity of keychains available: 4\n",
"Enter the number of customer orders: -1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of orders must be positive. Please try again.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the number of customer orders: 2\n",
"Enter the name of a product that a customer wants to order: pencil\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Product not found. Please enter a valid product.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of a product that a customer wants to order: book\n",
"Enter the name of a product that a customer wants to order: mug\n",
"Enter the price of ['t-shirt', 'mug', 'hat', 'book', 'keychain']: -4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Price cannot be negative. Please enter a valid price.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price of ['t-shirt', 'mug', 'hat', 'book', 'keychain']: 4.56\n",
"Enter the price of ['t-shirt', 'mug', 'hat', 'book', 'keychain']: 13.99\n"
]
},
{
"data": {
"text/plain": [
"18.55"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inventory = initialize_inventory(products)\n",
"\n",
"customer_orders = get_customer_orders(inventory)\n",
"total_price(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cade2383-c4b2-41d2-8eeb-2611375ec1e1",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -90,7 +273,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down