Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lab-python-error-handling #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
145 changes: 140 additions & 5 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,153 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "cc2c441d-9dcf-4817-b097-cf6cbe440846",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Successfully added 20 apples to inventory.\n",
"Completed processing for apple.\n",
"Successfully added 18 bananas to inventory.\n",
"Completed processing for banana.\n",
"Successfully added 32 oranges to inventory.\n",
"Completed processing for orange.\n",
"Inventory initialized: {'apple': 20, 'banana': 18, 'orange': 32}\n",
"Successfully added apple price to total.\n",
"Completed processing for apple.\n",
"Successfully added banana price to total.\n",
"Completed processing for banana.\n",
"Successfully added orange price to total.\n",
"Completed processing for orange.\n",
"Total price: 10.0\n",
"Final validation of order quantity.\n",
"Error: Product 'Apple' not found in inventory. Please enter a valid product.\n",
"Finalizing order for Apple.\n",
"Successfully placed order for 5 bananas.\n",
"Finalizing order for banana.\n",
"Orders placed: [('banana', 5)]\n",
"Remaining inventory: {'apple': 20, 'banana': 13, 'orange': 32}\n",
"The program completed successfully.\n",
"Program execution has finished.\n"
]
}
],
"source": [
"# your code goes here"
"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.\")\n",
" valid_quantity = True\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
" else:\n",
" print(f\"Successfully added {quantity} {product}s to inventory.\")\n",
" finally:\n",
" print(f\"Completed processing for {product}.\")\n",
" inventory[product] = quantity\n",
" return inventory\n",
"def calculate_total_price(products):\n",
" total_price = 0\n",
" for product in products:\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(\"Price cannot be negative. Please enter a valid price.\")\n",
" total_price += price\n",
" valid_price = True\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
" else:\n",
" print(f\"Successfully added {product} price to total.\")\n",
" finally:\n",
" print(f\"Completed processing for {product}.\")\n",
" return total_price\n",
"def get_customer_orders(inventory):\n",
" orders = []\n",
" while True:\n",
" try:\n",
" num_orders = int(input(\"Enter the number of orders: \"))\n",
" if num_orders < 0:\n",
" raise ValueError(\"The number of orders cannot be negative. Please enter a valid number.\")\n",
" break # exit loop when a valid number is entered\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
" else:\n",
" print(f\"Successfully entered {num_orders} orders.\")\n",
" finally:\n",
" print(\"Final validation of order quantity.\")\n",
"\n",
" for _ in range(num_orders):\n",
" valid_product = False\n",
" while not valid_product:\n",
" try:\n",
" product_name = input(\"Enter the product name: \")\n",
" if product_name not in inventory:\n",
" raise ValueError(f\"Product '{product_name}' not found in inventory. Please enter a valid product.\")\n",
" if inventory[product_name] <= 0:\n",
" raise ValueError(f\"Sorry, {product_name} is out of stock.\")\n",
" \n",
" quantity = int(input(f\"Enter the quantity of {product_name}: \"))\n",
" if quantity <= 0:\n",
" raise ValueError(\"Quantity must be greater than zero.\")\n",
" if quantity > inventory[product_name]:\n",
" raise ValueError(f\"Sorry, we only have {inventory[product_name]} of {product_name}. Please enter a valid quantity.\")\n",
" \n",
" orders.append((product_name, quantity))\n",
" inventory[product_name] -= quantity # Decrease inventory\n",
" valid_product = True\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
" else:\n",
" print(f\"Successfully placed order for {quantity} {product_name}s.\")\n",
" finally:\n",
" print(f\"Finalizing order for {product_name}.\")\n",
" \n",
" return orders\n",
"def main():\n",
" try:\n",
" # Sample products\n",
" products = ['apple', 'banana', 'orange']\n",
"\n",
" # Initialize inventory\n",
" inventory = initialize_inventory(products)\n",
" print(f\"Inventory initialized: {inventory}\")\n",
"\n",
" # Get total price of products\n",
" total_price = calculate_total_price(products)\n",
" print(f\"Total price: {total_price}\")\n",
"\n",
" # Get customer orders\n",
" orders = get_customer_orders(inventory)\n",
" print(f\"Orders placed: {orders}\")\n",
" print(f\"Remaining inventory: {inventory}\")\n",
" \n",
" except Exception as error:\n",
" print(f\"An unexpected error occurred: {error}\")\n",
" else:\n",
" print(\"The program completed successfully.\")\n",
" finally:\n",
" print(\"Program execution has finished.\")\n",
"\n",
"# Run the main function to start the program\n",
"main()\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -66,7 +201,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.0"
}
},
"nbformat": 4,
Expand Down