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
306 changes: 305 additions & 1 deletion lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,310 @@
"\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": 11,
"id": "5cca2ecb-57bd-43bb-b0cd-3f0f2f02f897",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: t\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input. Please enter an integer.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: we\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input. Please enter an integer.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: 3\n",
"Enter the quantity of mugs available: 5\n",
"Enter the quantity of hats available: six\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input. Please enter an integer.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of hats available: 6\n",
"Enter the quantity of books available: 5\n",
"Enter the quantity of keychains available: 0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Quantity of products must be greater than 0.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of keychains available: 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initial inventory: {'t-shirt': 3, 'mug': 5, 'hat': 6, 'book': 5, 'keychain': 1}\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the number of products you want to order: cinco\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input. Please enter the amount in the form of a whole number.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the number of products you want to order: 2.0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input. Please enter the amount in the form of a whole number.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the number of products you want to order: 2\n",
"Enter the name of the product you want to order: mUg\n",
"Enter the name of the product you want to order: h1t\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter a valid product with stock available:dict_items([('t-shirt', 3), ('mug', 5), ('hat', 6), ('book', 5), ('keychain', 1)])\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of the product you want to order: mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"You already added this product to your order.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of the product you want to order: hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer order: ['mug', 'hat']\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price of this product mug: three\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input. Please enter a number and use '.' for decimals.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price of this product mug: 5\n",
"Enter the price of this product hat: 6,7\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input. Please enter a number and use '.' for decimals.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price of this product hat: 6.7\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total price of the order: 11.7\n",
"Updated inventory:\n",
"t-shirt: 3\n",
"mug: 4\n",
"hat: 5\n",
"book: 5\n",
"keychain: 1\n"
]
},
{
"data": {
"text/plain": [
"[None, None, None, None, None]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"products=['t-shirt','mug','hat','book','keychain']\n",
"\n",
"def initialize_inventory(products):\n",
" up_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: \").strip())\n",
" if quantity <= 0:\n",
" print(\"Quantity of products must be greater than 0.\")\n",
" else:\n",
" up_inventory[product] = quantity\n",
" valid_input = True\n",
" except:\n",
" print(\"Invalid input. Please enter an integer.\")\n",
" return up_inventory\n",
"\n",
"def get_customer_order(inventory):\n",
" valid_number_prod=False\n",
" while not valid_number_prod:\n",
" try:\n",
" number_of_prod=int(input('Enter the number of products you want to order:').strip())\n",
" if number_of_prod<=0:\n",
" print('To place an order please introduce a number of products greater than 0.')\n",
" else:\n",
" valid_number_prod=True\n",
" except:\n",
" print(\"Invalid input. Please enter the amount in the form of a whole number.\")\n",
" customer_order=[]\n",
" for order in range(number_of_prod):\n",
" valid_order=False\n",
" while not valid_order:\n",
" try:\n",
" product=str(input('Enter the name of the product you want to order: ').strip()).lower()\n",
" if product not in inventory or inventory[product]<=0:\n",
" print(f'Please enter a valid product with stock available:{inventory.items()}')\n",
" elif product in customer_order:\n",
" print('You already added this product to your order.')\n",
" else:\n",
" customer_order.append(product)\n",
" valid_order=True\n",
" except:\n",
" print(\"Invalid input. Please enter the name of a product.\") \n",
" return customer_order\n",
"\n",
"def calculate_price(customer_order):\n",
" prices=[]\n",
" for product in customer_order:\n",
" valid_prices=False\n",
" while not valid_prices:\n",
" try:\n",
" price=float(input(f'Enter the price of this product {product}:').strip())\n",
" if price <= 0:\n",
" print(\"The price of the product must be greater than 0.\")\n",
" else:\n",
" prices.append(price)\n",
" valid_prices = True\n",
" except:\n",
" print(\"Invalid input. Please enter a number and use '.' for decimals.\")\n",
" total_price = sum(prices)\n",
" return total_price \n",
"\n",
"def calculate_order_statistics(customer_order, products):\n",
" total = len(customer_order)\n",
" percentage = (total / len(products)) * 100\n",
" print(\"Order Statistics:\")\n",
" print(f\"Total Products Ordered: {total}\")\n",
" print(f\"Percentage of Unique Products Ordered: {percentage}\")\n",
" return total, percentage\n",
"\n",
"def update_inventory(customer_order, inventory):\n",
" updated_inventory={product:(value-1 if product in customer_order else value) for product, value in inventory.items() \n",
" if (value - 1 if product in customer_order else value) > 0}\n",
" return updated_inventory\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated inventory:\")\n",
" final_inventory=[ print(f\"{product}: {quantity}\") for product, quantity in inventory.items()]\n",
" return final_inventory\n",
"\n",
"inventory = initialize_inventory(products)\n",
"print(f'Initial inventory: {inventory}')\n",
"customer_order =get_customer_order(inventory)\n",
"print(f'Customer order: {customer_order}')\n",
"total_price = calculate_price(customer_order)\n",
"print(f'Total price of the order: {total_price}')\n",
"inventory = update_inventory(customer_order, inventory)\n",
"print_updated_inventory(inventory)\n"
]
}
],
"metadata": {
Expand All @@ -90,7 +394,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.4"
}
},
"nbformat": 4,
Expand Down