Skip to content

lab completed #411

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

Open
wants to merge 1 commit into
base: main
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
85 changes: 84 additions & 1 deletion lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,94 @@
"\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": 20,
"id": "aca75df0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Apples': 32, 'Oranges': 23, 'lettuce': 1}\n",
"{'Apples': 23, 'Oranges': 21, 'lettuce': 32}\n",
"That brings your total up to: 23\n",
"That brings your total up to: 44\n",
"That brings your total up to: 76\n"
]
}
],
"source": [
"products = [\"Apples\", \"Oranges\", \"lettuce\"]\n",
"\n",
"\n",
"\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}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",
" inventory[product] = quantity\n",
" return inventory\n",
"\n",
"def initialize_prices(products):\n",
" prices = {}\n",
" for product in products:\n",
" valid_price = False\n",
" while not valid_price:\n",
" try:\n",
" price = int(input(f\"Enter the price of {product}s: \"))\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(f\"Error: {error}\")\n",
" prices[product] = price\n",
" return prices\n",
"\n",
"\n",
"\n",
"# inventory = initialize_inventory(products)\n",
"print(inventory)\n",
"#prices = initialize_prices(products)\n",
"print(prices)\n",
"\n",
"\n",
"\n",
"def get_customer_orders(inventory, prices):\n",
" total_price = 0\n",
" \n",
" for product in inventory:\n",
" valid_order = False\n",
" while not valid_order:\n",
" try:\n",
" order_quantity = int(input(f\"There are {inventory[product]} {product} for {prices[product]}€ each available- How many would you like to purchase?\"))\n",
" if order_quantity < 0:\n",
" raise ValueError(\"Invalid order Quantity! Please enter a non-negative value.\")\n",
" valid_order = True\n",
" total_price += order_quantity * prices[product]\n",
" except ValueError as error: \n",
" print(f\"Error: {error}\") \n",
" \n",
" print(f\"That brings your total up to: {total_price}\")\n",
"\n",
"\n",
"get_customer_orders(inventory, prices)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand Down