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
112 changes: 110 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,119 @@
"\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": 10,
"id": "190b1c99",
"metadata": {},
"outputs": [],
"source": [
"#1: Define the function for initializing the inventory with error handling\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(f\"Invalid quantity: {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": 11,
"id": "f7493b3e",
"metadata": {},
"outputs": [],
"source": [
"#2 Modification of the calculate_total_price function to include error handling:\n",
"def calculate_total_price (customer_orders):\n",
" price_product = []\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 (f\"Invalid price: {price}. Please enter a non-negative value.\")\n",
" valid_price = True\n",
" price_product.append(price)\n",
" except ValueError as error:\n",
" print(f\"Error: {error}.\") \n",
" \n",
" return sum(price_product)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "3eb5d4a3",
"metadata": {},
"outputs": [],
"source": [
"#3 Modification of the get_customer_orders function to include error handling:\n",
"def get_customer_orders (inventory):\n",
" valid_num_order = False\n",
" while not valid_num_order:\n",
" try:\n",
" num_orders = int(input('Enter the number of customer orders: '))\n",
" if num_orders < 0:\n",
" raise ValueError (f\"Invalid number of orders: {num_orders}! Please enter a non-negative value.\")\n",
" valid_num_order = True\n",
" except ValueError as error:\n",
" print(f\"Error: {error}.\")\n",
"\n",
" customer_orders = []\n",
" for i in range(num_orders):\n",
" valid_product = False \n",
" while not valid_product:\n",
" try:\n",
" preference = input(\"Enter the name of a products that a customer wants to order:\")\n",
" if not (preference in inventory) or inventory[preference] < 0:\n",
" raise ValueError(f\"Sorry! {preference} is not available.\")\n",
" valid_product = True\n",
" customer_orders.append(preference) \n",
" except ValueError as error:\n",
" print(f\"Error: {error}.\") \n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "1f19e621",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: Invalid quantity: -2! Please enter a non-negative value.\n",
"Error: Invalid number of orders: -1! Please enter a non-negative value..\n",
"Error: could not convert string to float: 'g'.\n"
]
}
],
"source": [
"#4 Results \n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders (inventory)\n",
"total_price = calculate_total_price (customer_orders)\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +198,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.0"
}
},
"nbformat": 4,
Expand Down