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
210 changes: 209 additions & 1 deletion lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,214 @@
"\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": 57,
"id": "436f531c-b4ca-4986-94da-272cd461a36c",
"metadata": {},
"outputs": [],
"source": [
"# Step 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} 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\n"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "61dc3008-cc2f-4a19-9575-52b31870a560",
"metadata": {},
"outputs": [],
"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), \n",
"#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",
"\n",
"def calculate_total_price():\n",
" price_list= []\n",
" for product in products:\n",
" valid_price = False\n",
" while not valid_price:\n",
" try:\n",
" price = int(input(f\"Enter the Enter the price of: {product}\"))\n",
" if price <0:\n",
" raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n",
" else:\n",
" valid_price = True\n",
" price_list.append(price) \n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
" return sum(price_list) \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "fda579cd-67d6-493f-8095-c5bc453191c2",
"metadata": {},
"outputs": [],
"source": [
"#3 Modify the get_customer_orders function to include error handling.\n",
"\n",
"def get_customer_orders(inventory):\n",
" customer_orders = set()\n",
" valid_digit = False\n",
" while not valid_digit:\n",
" try:\n",
" order_number = int(input(\"Enter the number of customer orders: \"))\n",
" if order_number <0:\n",
" raise ValueError(\"Invalid order number! Please enter a non-negative value.\")\n",
" else:\n",
" valid_digit = True \n",
" except ValueError as error:\n",
" print(f\"Error: {error}\") \n",
"\n",
" for i in range(0, order_number):\n",
" product_not_available = True\n",
" while product_not_available:\n",
" try:\n",
" order = input(\"Enter the name of a product that a customer wants to order: \")\n",
" if order not in inventory:\n",
" raise ValueError(\"product not available, please choose another one.\")\n",
" elif inventory.get(order) == 0:\n",
" raise ValueError(\"product not in stock, please choose another one.\") \n",
" else:\n",
" customer_orders.add(order)\n",
" product_not_available = False\n",
" except (NameError,ValueError) as error:\n",
" print(f\"Error: {error}\")\n",
" \n",
" return customer_orders \n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "ef76be67-3026-4a96-a7cc-aef61ffa8b68",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"add a list of products separated by , book,hat,mug\n",
"Enter the quantity of book available: 11\n",
"Enter the quantity of hat available: 0\n",
"Enter the quantity of mug available: 11\n",
"Enter the Enter the price of: book 11\n",
"Enter the Enter the price of: hat 11\n",
"Enter the Enter the price of: mug 11\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"the price is 33\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: book\n",
"Enter the name of a product that a customer wants to order: kkkk\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: product not available, please choose another one.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of a product that a customer wants to order: aa\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: product not available, please choose another one.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of a product that a customer wants to order: hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: product not in stock, please choose another one.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of a product that a customer wants to order: mug\n"
]
},
{
"data": {
"text/plain": [
"{'book', 'mug'}"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#4 Test your code by running the program and deliberately entering invalid quantities and product names. \n",
"#Make sure the error handling mechanism works as expected.\n",
"\n",
"user_products = input(\"add a list of products separated by ,\")\n",
"try:\n",
" products = user_products.strip().split(\",\")\n",
"except:\n",
" print(\"error in creating products\")\n",
"\n",
"inventory= initialize_inventory(products)\n",
"price = calculate_total_price()\n",
"print(f\"the price is {price}\")\n",
"get_customer_orders(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c267f47b-79cd-4ae7-8784-150de9b3c13a",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -90,7 +298,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down