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
297 changes: 294 additions & 3 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,304 @@
"\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": "30baa24e-7c01-4838-a6a3-4b3d0b04aa48",
"metadata": {},
"outputs": [],
"source": [
"products=['t-shirt','mug','hat','book','keychain']\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "b8bfe769-a9b9-4ea1-b97e-6cfaba76c457",
"metadata": {},
"outputs": [],
"source": [
"# 2 Modify the `calculate_total_price` function to include error handling.\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" \n",
" for product in products:\n",
" while True:\n",
" quantity = input(\"Enter the quantity of available \" + product + \"s : \")\n",
" \n",
" if quantity.isdigit():\n",
" inventory[product] = int(quantity)\n",
" break \n",
" else:\n",
" print(\"Invalid input, please try again\")\n",
" \n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "1a5db72a-8640-4d6e-8b9e-6a2da1ae768f",
"metadata": {},
"outputs": [],
"source": [
"# 3\n",
"\n",
"def get_customer_order(inventory):\n",
" while True:\n",
" total_input = input(\"Enter the number of products you want to order:\").strip()\n",
" \n",
" if total_input.isdigit():\n",
" number_of_prod = int(total_input)\n",
" if number_of_prod > 0:\n",
" break \n",
" else:\n",
" print(\"To place an order please introduce a number of products greater than 0.\")\n",
" else:\n",
" print(\"Invalid input. Please enter the amount in the form of a whole number.\")\n",
"\n",
" customer_order = []\n",
" \n",
" \n",
" while len(customer_order) < number_of_prod:\n",
" product = input(\"Enter the name of the product you want to order: \").strip().lower()\n",
" \n",
" \n",
" if product not in inventory or inventory[product] <= 0:\n",
" \n",
" print(\"Please enter a valid product with stock available:\" + str(list(inventory.items())))\n",
" \n",
" \n",
" elif product in customer_order:\n",
" print(\"You already added this product to your order.\")\n",
" \n",
" else:\n",
" customer_order.append(product)\n",
" \n",
" return customer_order"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "10c913f3-07c8-411d-a627-1249757f6af6",
"metadata": {},
"outputs": [],
"source": [
"def calculate_price(customer_order):\n",
" prices = []\n",
" for product in customer_order:\n",
" while True:\n",
" price_input = input(\"Enter the price of this product \" + product + \": \").strip()\n",
" \n",
" if price_input.replace(\".\", \"\", 1).isdigit():\n",
" price = float(price_input)\n",
" if price > 0:\n",
" prices.append(price)\n",
" break \n",
" else:\n",
" print(\"The price of the product must be greater than 0.\")\n",
" else:\n",
" print(\"Invalid input. Please enter a number and use '.' for decimals.\")\n",
" \n",
" total_price = sum(prices)\n",
" return total_price\n",
"\n",
"\n",
"\n",
"\n",
"def calculate_order_statistics(customer_order, products):\n",
" total = len(customer_order)\n",
" percentage = (total / len(products)) * 100\n",
" \n",
" print(\"Order Statistics:\")\n",
" print(\"Total Products Ordered: \" + str(total))\n",
" print(\"Percentage of Unique Products Ordered: \" + str(percentage))\n",
" \n",
" return total, percentage\n",
"\n",
"\n",
"def update_inventory(customer_order, inventory):\n",
" updated_inventory = {}\n",
" \n",
" for product in inventory:\n",
" value = inventory[product]\n",
" \n",
" if product in customer_order:\n",
" new_value = value - 1\n",
" else:\n",
" new_value = value\n",
" \n",
" if new_value > 0:\n",
" updated_inventory[product] = new_value\n",
" \n",
" return updated_inventory\n",
"\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated inventory:\")\n",
" \n",
" final_inventory = []\n",
" for product in inventory:\n",
" quantity = inventory[product]\n",
" print(product + \": \" + str(quantity))\n",
" \n",
" return final_inventory"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "6bbadfe9-9ce4-4312-abc5-3f7726133427",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--- STEP 1: INITIALIZING INVENTORY ---\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of available t-shirts : 6\n",
"Enter the quantity of available mugs : 8\n",
"Enter the quantity of available hats : 9\n",
"Enter the quantity of available books : 25\n",
"Enter the quantity of available keychains : 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initial Inventory Dictionary: {'t-shirt': 6, 'mug': 8, 'hat': 9, 'book': 25, 'keychain': 1}\n",
"----------------------------------------\n",
"\n",
"--- STEP 2: GETTING CUSTOMER ORDER ---\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the number of products you want to order: 4\n",
"Enter the name of the product you want to order: mug \n",
"Enter the name of the product you want to order: hat\n",
"Enter the name of the product you want to order: book\n",
"Enter the name of the product you want to order: dog\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter a valid product with stock available:[('t-shirt', 6), ('mug', 8), ('hat', 9), ('book', 25), ('keychain', 1)]\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of the product you want to order: t-shirt\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer Order List: ['mug', 'hat', 'book', 't-shirt']\n",
"----------------------------------------\n",
"\n",
"--- STEP 3: CALCULATING TOTAL PRICE ---\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price of this product mug: 6\n",
"Enter the price of this product hat: 8\n",
"Enter the price of this product book: 19\n",
"Enter the price of this product t-shirt: 25\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Price to Pay: 58.0\n",
"----------------------------------------\n",
"\n",
"--- STEP 4: SHOWING ORDER STATISTICS ---\n",
"Order Statistics:\n",
"Total Products Ordered: 4\n",
"Percentage of Unique Products Ordered: 80.0\n",
"----------------------------------------\n",
"\n",
"--- STEP 5: UPDATING INVENTORY STOCK ---\n",
"----------------------------------------\n",
"\n",
"--- STEP 6: PRINTING THE FINAL INVENTORY ---\n",
"Updated inventory:\n",
"t-shirt: 5\n",
"mug: 7\n",
"hat: 8\n",
"book: 24\n",
"keychain: 1\n",
"=========================================\n"
]
}
],
"source": [
"products=['t-shirt','mug','hat','book','keychain']\n",
"\n",
"print(\"--- STEP 1: INITIALIZING INVENTORY ---\")\n",
"current_inventory = initialize_inventory(products)\n",
"print(\"Initial Inventory Dictionary:\", current_inventory)\n",
"print(\"-\" * 40)\n",
"\n",
"print(\"\\n--- STEP 2: GETTING CUSTOMER ORDER ---\")\n",
"order = get_customer_order(current_inventory)\n",
"print(\"Customer Order List:\", order)\n",
"print(\"-\" * 40)\n",
"\n",
"print(\"\\n--- STEP 3: CALCULATING TOTAL PRICE ---\")\n",
"total_to_pay = calculate_price(order)\n",
"print(\"Total Price to Pay: \" + str(total_to_pay))\n",
"print(\"-\" * 40)\n",
"\n",
"print(\"\\n--- STEP 4: SHOWING ORDER STATISTICS ---\")\n",
"stats = calculate_order_statistics(order, products)\n",
"print(\"-\" * 40)\n",
"\n",
"print(\"\\n--- STEP 5: UPDATING INVENTORY STOCK ---\")\n",
"current_inventory = update_inventory(order, current_inventory)\n",
"print(\"-\" * 40)\n",
"\n",
"print(\"\\n--- STEP 6: PRINTING THE FINAL INVENTORY ---\")\n",
"print_updated_inventory(current_inventory)\n",
"print(\"=========================================\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5e27ec16-842f-4961-83e4-9cf2f447a80d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -90,7 +381,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down