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
125 changes: 122 additions & 3 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,132 @@
"\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": 26,
"id": "d7b3e227-93a6-48b9-85c6-a0a366c2e8b3",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price of hat: -10\n",
"Enter the price of keychain: 9\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input. Prices cannot be negative.\n"
]
},
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def calculate_total_price(customer_orders):\n",
" try:\n",
" prices = {product: float(input(f\"Enter the price of {product}: \")) for product in customer_orders}\n",
"\n",
" if any(price < 0 or price == str for price in prices.values()):\n",
" print(\"Invalid input. Prices cannot be negative.\")\n",
" return 0\n",
"\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter numeric values for prices.\")\n",
" return 0\n",
" else:\n",
" return sum(prices.values())\n",
" \n",
"orders = [\"hat\", \"keychain\"]\n",
"calculate_total_price(orders)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "f8fc2ec0-04c6-469f-8455-aff10012355d",
"metadata": {},
"outputs": [
{
"ename": "KeyboardInterrupt",
"evalue": "Interrupted by user",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mKeyboardInterrupt\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[73]\u001b[39m\u001b[32m, line 34\u001b[39m\n\u001b[32m 30\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m customer_orders\n\u001b[32m 32\u001b[39m inventory = {\u001b[33m'\u001b[39m\u001b[33mt-shirt\u001b[39m\u001b[33m'\u001b[39m: \u001b[32m3\u001b[39m, \u001b[33m'\u001b[39m\u001b[33mmug\u001b[39m\u001b[33m'\u001b[39m: \u001b[32m0\u001b[39m, \u001b[33m'\u001b[39m\u001b[33mhat\u001b[39m\u001b[33m'\u001b[39m: \u001b[32m1\u001b[39m, \u001b[33m'\u001b[39m\u001b[33mbook\u001b[39m\u001b[33m'\u001b[39m: \u001b[32m3\u001b[39m, \u001b[33m'\u001b[39m\u001b[33mkeychain\u001b[39m\u001b[33m'\u001b[39m: \u001b[32m4\u001b[39m}\n\u001b[32m---> \u001b[39m\u001b[32m34\u001b[39m orders = \u001b[43mget_customer_orders\u001b[49m\u001b[43m(\u001b[49m\u001b[43minventory\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 35\u001b[39m \u001b[38;5;28mprint\u001b[39m(orders)\n",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[73]\u001b[39m\u001b[32m, line 5\u001b[39m, in \u001b[36mget_customer_orders\u001b[39m\u001b[34m(inventory)\u001b[39m\n\u001b[32m 3\u001b[39m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[32m 4\u001b[39m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[32m----> \u001b[39m\u001b[32m5\u001b[39m num_orders = \u001b[38;5;28mint\u001b[39m(\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mEnter the number of customer orders: \u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m)\n\u001b[32m 6\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m num_orders < \u001b[32m0\u001b[39m:\n\u001b[32m 7\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mInvalid input: number must be positive.\u001b[39m\u001b[33m\"\u001b[39m)\n",
"\u001b[36mFile \u001b[39m\u001b[32m/opt/anaconda3/lib/python3.13/site-packages/ipykernel/kernelbase.py:1275\u001b[39m, in \u001b[36mKernel.raw_input\u001b[39m\u001b[34m(self, prompt)\u001b[39m\n\u001b[32m 1273\u001b[39m msg = \u001b[33m\"\u001b[39m\u001b[33mraw_input was called, but this frontend does not support input requests.\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 1274\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m StdinNotImplementedError(msg)\n\u001b[32m-> \u001b[39m\u001b[32m1275\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_input_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 1276\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mstr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mprompt\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1277\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_parent_ident\u001b[49m\u001b[43m[\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mshell\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1278\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mget_parent\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mshell\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1279\u001b[39m \u001b[43m \u001b[49m\u001b[43mpassword\u001b[49m\u001b[43m=\u001b[49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[32m 1280\u001b[39m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
"\u001b[36mFile \u001b[39m\u001b[32m/opt/anaconda3/lib/python3.13/site-packages/ipykernel/kernelbase.py:1320\u001b[39m, in \u001b[36mKernel._input_request\u001b[39m\u001b[34m(self, prompt, ident, parent, password)\u001b[39m\n\u001b[32m 1317\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m:\n\u001b[32m 1318\u001b[39m \u001b[38;5;66;03m# re-raise KeyboardInterrupt, to truncate traceback\u001b[39;00m\n\u001b[32m 1319\u001b[39m msg = \u001b[33m\"\u001b[39m\u001b[33mInterrupted by user\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m-> \u001b[39m\u001b[32m1320\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m(msg) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[32m 1321\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[32m 1322\u001b[39m \u001b[38;5;28mself\u001b[39m.log.warning(\u001b[33m\"\u001b[39m\u001b[33mInvalid Message:\u001b[39m\u001b[33m\"\u001b[39m, exc_info=\u001b[38;5;28;01mTrue\u001b[39;00m)\n",
"\u001b[31mKeyboardInterrupt\u001b[39m: Interrupted by user"
]
}
],
"source": [
"def get_customer_orders(inventory):\n",
"\n",
" while True:\n",
" try:\n",
" num_orders = int(input(\"Enter the number of customer orders: \"))\n",
" if num_orders < 0:\n",
" print(\"Invalid input: number must be positive.\")\n",
" elif num_orders == 0:\n",
" print(\"No customer orders\")\n",
" else:\n",
" break\n",
" except ValueError:\n",
" print(\"Invalid input: please enter a numeric value.\")\n",
"\n",
" customer_orders = set()\n",
"\n",
"\n",
" for i in range(num_orders):\n",
" while True:\n",
" product = input(f\"Enter the name of product #{i + 1}: \")\n",
"\n",
" if product not in inventory:\n",
" print(\"Invalid product: not in inventory.\")\n",
" elif inventory[product] <= 0:\n",
" print(\"Invalid product: out of stock.\")\n",
" else:\n",
" customer_orders.add(product)\n",
" break\n",
"\n",
" return customer_orders\n",
"\n",
"inventory = {'t-shirt': 3, 'mug': 0, 'hat': 1, 'book': 3, 'keychain': 4}\n",
"\n",
"orders = get_customer_orders(inventory)\n",
"print(orders)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4e56e926-cfc6-4ce5-9693-f4291fb3a4b4",
"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 +209,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down