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": 8,
"id": "a9d1631d",
"metadata": {},
"outputs": [],
"source": [
"# Step 1: Define the function for initializing the inventory with error handling\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"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: \"))\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": "code",
"execution_count": 10,
"id": "37e51b90",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Total customer orders 2:\n",
"hat\n",
"mug\n"
]
}
],
"source": [
"# Modify the `get_customer_orders` function to include error handling.\n",
" #- If the user enters an invalid number of orders (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the number of orders.\n",
" #- 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",
"def get_customer_orders():\n",
"\n",
" number_of_orders = int(input(\"please enter the number of customer orders:\")) \n",
" \n",
" customer_orders = set()\n",
" for i in range(number_of_orders):\n",
" valid_product = False\n",
" \n",
" while not valid_product:\n",
" try:\n",
" product_name = input(f\"Enter product name for order {i + 1}: \") \n",
" if product_name not in products:\n",
" raise ValueError(\"invalid product name, please enter the correct product name\")\n",
" valid_product = True\n",
" customer_orders.add(product_name)\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
"\n",
" return customer_orders, number_of_orders\n",
"\n",
"\n",
"customer_orders, number_of_orders = get_customer_orders()\n",
"\n",
"print(f\"\\nTotal customer orders {number_of_orders}:\")\n",
"for order in customer_orders:\n",
" print(order)\n",
"\n",
"#print(f\"\\nTotal customer orders {number_of_orders}:\")\n",
"#"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "af9e8c0a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The price for hat is: 13.50Euro\n",
"The price for mug is: 16.50Euro\n",
"\n",
"The total price of the customers order is: 30.00 Euro\n"
]
}
],
"source": [
"#2. Modify the `calculate_total_price` function to include error handling.\n",
" # If the user enters an invalid price (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the price for that product.\n",
" # Use a try-except block to handle the error and continue prompting the user until a valid price is entered.\n",
"#define prices_per_product\n",
"def price_per_product(customer_orders):\n",
" product_prices = {}\n",
" #{product: float(input(f\"Please enter the price of {product}: \")) for product in customer_orders}\n",
" for product in customer_orders:\n",
" valid_price = False\n",
" while not valid_price:\n",
" try:\n",
" price = float(input(f\"Please enter the price of {product}: \"))\n",
" \n",
" if price < 0:\n",
" raise ValueError(f\"Invalid price! Please enter the correct price for {product}\")\n",
" valid_price = True\n",
" product_prices[product] = price\n",
"\n",
" except ValueError as error:\n",
" print(error)\n",
" \n",
" return product_prices\n",
"\n",
"#calculate total price\n",
"def total_price(customer_orders):\n",
" return sum(prices.values())\n",
"\n",
"\n",
"prices = price_per_product(customer_orders)\n",
"total = total_price(prices)\n",
" \n",
" \n",
"#print prices\n",
"for product, price in prices.items():\n",
" print(f\"The price for {product} is: {price:.2f}Euro\")\n",
"#Print the total price of the customer order.\n",
"print(f\"\\nThe total price of the customers order is: {total:.2f} Euro\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7dad61d8",
"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.14.2"
}
},
"nbformat": 4,
Expand Down