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
324 changes: 320 additions & 4 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,334 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 39,
"id": "8cd86061",
"metadata": {},
"outputs": [],
"source": [
"import string"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "0050200b",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "cc2c441d-9dcf-4817-b097-cf6cbe440846",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"def initialize_inventory(products):\n",
" inventory = {}\n",
"\n",
" for product in products:\n",
" try:\n",
" quantity = int(input(f\"Enter a quantity of {product}: \"))\n",
" \n",
" if quantity < 0:\n",
" raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n",
" \n",
" inventory[product] = quantity\n",
" \n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
" inventory[product] = 0\n",
"\n",
" return inventory\n"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "eb9c615c",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_order():\n",
" customer_orders = set()\n",
"\n",
" while True:\n",
" try:\n",
" order = input(\"Enter a product to order or 'exit' to finish: \")\n",
"\n",
" if order == \"exit\":\n",
" break\n",
" if not order:\n",
" raise ValueError(\"Order name cannot be empty.\")\n",
" if order not in customer_orders:\n",
" raise ValueError(f\"Product '{order}' doesnt exits\")\n",
"\n",
"\n",
" customer_orders.add(order)\n",
"\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
"\n",
" except KeyboardInterrupt:\n",
" print(\"\\nOperation cancelled\") \n",
" break \n",
"\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "63f944e1",
"metadata": {},
"outputs": [],
"source": [
"def calculate_total_price(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" try:\n",
" percentage = (total_products_ordered / len(products)) *100\n",
" except ZeroDivisionError:\n",
" print(\"Error: No products to calculate percentage\")\n",
" percentage = 0\n",
" else:\n",
" print(\"Statistics calculated\")\n",
" \n",
" return total_products_ordered, percentage "
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "e127e618",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" for order in customer_orders:\n",
" try:\n",
" if order not in inventory:\n",
" raise KeyError(f\"Product {order} not found\")\n",
" if inventory[order] > 0:\n",
" inventory[order] -= 1\n",
" else:\n",
" print(f\"Product out of stock\")\n",
" except KeyError as error:\n",
" print(f\"Error: {error}\")\n",
" return inventory\n"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "b37f7a27",
"metadata": {},
"outputs": [],
"source": [
"def remove_punctuation(sentence):\n",
" if not isinstance(sentence,str):\n",
" raise ValueError(\"Input must be a string.\")\n",
" return sentence.translate(str.maketrans(\"\", \"\", string.punctuation))"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "af6d4fbc",
"metadata": {},
"outputs": [],
"source": [
"def word_count(sentence):\n",
" try:\n",
" sentence = remove_punctuation(sentence)\n",
" words = sentence.split()\n",
" except ValueError as error:\n",
" print(error)\n",
" else: \n",
" return len(words)\n",
" finally:\n",
" print(\"Execution complete\")"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "17d8e01a",
"metadata": {},
"outputs": [],
"source": [
"def divide (x, y):\n",
" try:\n",
" result = x / y\n",
" except TypeError:\n",
" raise TypeError(\"Invalid arguments\")\n",
" except ZeroDivisionError:\n",
" raise ZeroDivisionError(\"Cannot divide by zero\")\n",
" else:\n",
" return result\n",
" finally:\n",
" print(\"Division executed\")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "b3de9425",
"metadata": {},
"outputs": [],
"source": [
"def fibonacci(number):\n",
" try:\n",
" if number < 0:\n",
" raise ValueError(\"The input must be non-negative\")\n",
" if number <=1:\n",
" return number\n",
" else :\n",
" return fibonacci(number - 1) + fibonacci(number - 2)\n",
" except TypeError:\n",
" raise TypeError(\"Must be an integer\")\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "da6be2ec",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: invalid literal for int() with base 10: 'd'\n",
"{'t-shirt': 0, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n"
]
}
],
"source": [
"inventory = initialize_inventory (products)\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "a3181dee",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: Product 'cat' doesnt exits\n",
"{'hat', 'mug', 'book'}\n"
]
}
],
"source": [
"customer_orders = get_customer_order()\n",
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "82553983",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 1, 'mug': 1, 'hat': 2, 'book': 3, 'keychain': 5}\n"
]
}
],
"source": [
"updated_inventory = update_inventory(customer_orders, inventory)\n",
"print(updated_inventory)"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "4809157c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Statistics calculated\n",
"(3, 60.0)\n"
]
}
],
"source": [
"order_statistics = calculate_total_price(customer_orders, products)\n",
"print(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "0578169a",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics (order_statistics):\n",
" total_products_ordered, percentage = order_statistics\n",
"\n",
" print(f\"Total products ordered: {total_products_ordered}\")\n",
" print(f\"Percentage of products ordered: {percentage:.2f}\")"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "3381fbd5",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\") "
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "c3122461",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total products ordered: 3\n",
"Percentage of products ordered: 60.00\n",
"Updated Inventory:\n",
"t-shirt: 1\n",
"mug: 1\n",
"hat: 2\n",
"book: 3\n",
"keychain: 5\n"
]
}
],
"source": [
"final_statistics = print_order_statistics(order_statistics)\n",
"final_updated_inventory = print_updated_inventory(inventory)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -66,7 +382,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down