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
151 changes: 149 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,158 @@
"\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": "d544c39a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input:invalid literal for int() with base 10: 's'\n",
"Sorry, this product is out of stock. Please choose another product.\n",
"Products in customer orders: {'hat', 'book'}\n",
"Order Statistics\n",
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 40.0 %\n",
"Updated Inventory:\n",
"t-shirt: 2\n",
"mug: 1\n",
"hat: 3\n",
"book: 5\n",
"keychain: 0\n",
"Total Price: 102.0\n"
]
},
{
"data": {
"text/plain": [
"102.0"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#1 \n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"#2\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\n",
"#3\n",
"def get_customer_orders(inventory):\n",
" customer_orders = set()\n",
" \n",
" while True: \n",
" try:\n",
" num_orders = int(input(\"How many products would you like to order'? \"))\n",
" if num_orders < 0:\n",
" raise ValueError(\"Number of orders cannot be negative.\")\n",
" break\n",
" except ValueError as error:\n",
" print(f\"Invalid input:{error}\")\n",
"\n",
" for _ in range(num_orders):\n",
" while True:\n",
" product = input(\"Enter the product you want to order: \").strip().lower()\n",
" if product not in inventory:\n",
" print(\"Product not found in inventory. Please enter a valid product\")\n",
" elif inventory[product] <= 0:\n",
" print(\"Sorry, this product is out of stock. Please choose another product.\")\n",
" else:\n",
" customer_orders.add(product)\n",
" break\n",
" \n",
" \n",
" print(\"Products in customer orders:\", customer_orders)\n",
" return customer_orders\n",
"\n",
"\n",
"#4\n",
"def update_inventory(inventory,customer_orders):\n",
" inventory = {item: inventory[item] - 1 if item in customer_orders else inventory[item] for item in inventory}\n",
"\n",
" inventory = {item: qty for item, qty in inventory.items() if qty > 0}\n",
" for item, qty in inventory.items():\n",
" print(f\"{item}, Quantity Left: {qty}\")\n",
"\n",
" return inventory\n",
"\n",
"def price_calculator(customer_orders):\n",
" total_price = 0.0\n",
"\n",
" for product in customer_orders:\n",
" valid_price = False\n",
" while not valid_price:\n",
" try:\n",
" price = float(input(f\"Enter the price of {product}: \"))\n",
" if price < 0:\n",
" raise ValueError(\"Price cannot be negative.\")\n",
" total_price += price\n",
" valid_price = True\n",
" except ValueError as error:\n",
" print(f\"Invalid input: {error}\")\n",
"\n",
" print(f\"Total Price: {total_price}\")\n",
" return total_price\n",
"\n",
"\n",
"#5\n",
"def calculate_order_statistics(customer_orders,products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = (total_products_ordered / len(products)) * 100\n",
" order_status = (total_products_ordered, percentage_ordered)\n",
" return order_status\n",
"#6\n",
"def print_order_statistics(order_statistics):\n",
" print(\"Order Statistics\")\n",
" print(\"Total Products Ordered:\", order_statistics[0])\n",
" print(\"Percentage of Products Ordered:\", order_statistics[1], \"%\")\n",
"#7\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" for item, qty in inventory.items():\n",
" print(f\"{item}: {qty}\")\n",
"\n",
"\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders(inventory)\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory(inventory)\n",
"price_calculator(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "271ae85e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +237,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.2"
}
},
"nbformat": 4,
Expand Down