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
210 changes: 207 additions & 3 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,217 @@
"\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": 1,
"id": "47b226c1-c908-4f08-9cc7-e70d6e2f1ed8",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Put the pice for banana: 4\n",
"Put the pice for apple: \n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid entry. Please enter a numerc value.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Put the pice for apple: 5\n",
"Put the pice for pear: 6\n"
]
},
{
"data": {
"text/plain": [
"15"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"products = ['banana', 'apple', 'pear']\n",
"\n",
"def calculate_total_price(products):\n",
" totalprice = 0\n",
" for product in products:\n",
" validprice = False\n",
" while not validprice:\n",
" try:\n",
" price = float(input(f\"Put the pice for {product}: \"))\n",
" if price >= 0:\n",
" totalprice += price\n",
" validprice = True\n",
" else:\n",
" print(\"Error: enter a non-negative price.\")\n",
" except ValueError:\n",
" print(\"Invalid entry. Please enter a numerc value.\")\n",
" return int(totalprice)\n",
"\n",
"calculate_total_price(products)\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a26c9497-d9e1-4754-a118-e410a0f2ac22",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Number of orders: 3\n",
"Produto #1: 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid produtc\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Product #1: 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid produtc\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Product #1: r\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid produtc\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Product #1: 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid produtc\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Product #1: banana\n",
"Quantity of banana: 2\n",
"Produto #2: 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid produtc\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Product #2: banana\n",
"Quantity of banana: 4\n",
"Produto #3: apple\n",
"Quantity of apple: 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'banana': 4, 'apple': 4}\n"
]
}
],
"source": [
"inventory = {\"banana\": 10, \"apple\": 5, \"pear\": 8} \n",
"\n",
"orders = get_customer_orders(inventory)\n",
"print(orders)\n",
"\n",
"def get_customer_orders(inventory):\n",
" orders = {}\n",
"\n",
" while True:\n",
" try:\n",
" num_orders = int(input(\"Number of orders: \"))\n",
" if num_orders > 0:\n",
" break\n",
" print(\"Enter a positive number.\")\n",
" except ValueError:\n",
" print(\"Yu have to enter an integer number.\")\n",
" for i in range(num_orders):\n",
" product = input(f\"Produto #{i+1}: \").strip().lower()\n",
" while product not in inventory or inventory[product] <= 0:\n",
" print(\"Invalid produtc\")\n",
" product = input(f\"Product #{i+1}: \").strip().lower()\n",
"\n",
" while True:\n",
" try:\n",
" qty = int(input(f\"Quantity of {product}: \"))\n",
" if 0 < qty <= inventory[product]:\n",
" break\n",
" print(\"Invalid quantity.\")\n",
" except ValueError:\n",
" print(\"You have to enter an integer number.\")\n",
"\n",
" orders[product] = qty\n",
"\n",
" return orders\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "df12b8c5-5d8e-4d6b-9eee-0f56c6099037",
"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 +294,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down