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
121 changes: 120 additions & 1 deletion lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,125 @@
"\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": 6,
"id": "b89b3046",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: 1d\n",
"Enter a valid quantity number\n",
"Enter the quantity of t-shirts available: 10\n",
"Enter the quantity of mugs available: 10\n",
"Enter the quantity of hats available: 10\n",
"Enter the quantity of books available: 10\n",
"Enter the quantity of keychains available: 10\n",
"Enter the number of customer orders: d\n",
"Enter a valid number\n",
"Enter the number of customer orders: 2\n",
"Enter the name of a product that a customer wants to order: 1\n",
"Invalid product name or out of stock! Please enter a valid product name.\n",
"Enter the name of a product that a customer wants to order: hat\n",
"Enter the name of a product that a customer wants to order: mug\n",
"\n",
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Percentage of Unique Products Ordered: 100.0%\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 9\n",
"book: 9\n",
"keychain: 9\n",
"Enter the price of hat: l\n",
"Error: Enter a valid price number \n",
"Enter the price of hat: 19\n",
"Enter the price of mug: 19\n",
"Total Price: 38.0\n"
]
}
],
"source": [
"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.\") #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",
" valid_quantity = True\n",
" except ValueError as error: #Use a try-except block to handle the error and continue prompting the user until a valid price is entered.\n",
" print(\"Enter a valid quantity number\")\n",
" inventory[product] = quantity\n",
" return inventory\n",
"\n",
"def get_customer_orders(inventory):\n",
" valid_num_orders = False\n",
" while not valid_num_orders: #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",
" try:\n",
" num_orders = int(input(\"Enter the number of customer orders: \"))\n",
" if num_orders < 0:\n",
" raise ValueError(\"Invalid number of orders! Please enter a non-negative value.\")\n",
" valid_num_orders = True\n",
" except ValueError as error:\n",
" print(\"Enter a valid number\")\n",
" customer_orders = []\n",
" for _ in range(num_orders):\n",
" valid_product = False\n",
" while not valid_product:\n",
" product = input(\"Enter the name of a product that a customer wants to order: \")\n",
" if product not in inventory or inventory[product] == 0:\n",
" print(\"Invalid product name or out of stock! Please enter a valid product name.\") #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",
" else:\n",
" valid_product = True\n",
" customer_orders.append(product)\n",
" return customer_orders\n",
"\n",
"def calculate_total_price(customer_orders):\n",
" total_price = 0\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(\"Invalid price! Please enter a non-negative value.\")\n",
" valid_price = True\n",
" except ValueError as error:\n",
" print(\"Error: Enter a valid price number \")\n",
" total_price += price\n",
" return total_price\n",
"\n",
"def update_inventory(inventory, customer_orders):\n",
" inventory = {product: quantity - 1 for product, quantity in inventory.items() if product not in customer_orders}\n",
" return inventory\n",
"\n",
"def print_total_price(total_price):\n",
" print(f\"Total Price: {total_price}\")\n",
" \n",
"def main():\n",
" products = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
" inventory = initialize_inventory(products)\n",
" customer_orders = get_customer_orders(inventory)\n",
" print(\"\\nOrder Statistics:\")\n",
" print(f\"Total Products Ordered: {len(customer_orders)}\")\n",
" print(f\"Percentage of Unique Products Ordered: {len(set(customer_orders)) / len(customer_orders) * 100}%\")\n",
" print(\"\\nUpdated Inventory:\")\n",
" \n",
" inventory = update_inventory(inventory, customer_orders)\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
" total_price = calculate_total_price(customer_orders)\n",
" print_total_price(total_price)\n",
"main()\n",
"\n"
]
}
],
"metadata": {
Expand All @@ -90,7 +209,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.10.9"
}
},
"nbformat": 4,
Expand Down