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
139 changes: 137 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,146 @@
"\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": 3,
"id": "bc6cf873",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'f' is not available. Please choose from: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"'m' is not available. Please choose from: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"Total Ordered: 2\n",
"Percentagem of products ordered: 40.0\n",
"t-shirt: 10\n",
"mug: 10\n",
"hat: 9\n",
"book: 9\n",
"keychain: 10\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" valid_input = False\n",
" while not valid_input:\n",
" try:\n",
" quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n",
" if quantity >= 0:\n",
" inventory[product] = quantity\n",
" valid_input = True\n",
" else:\n",
" print(\"Quantity cannot be negative. Please enter a valid quantity.\")\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid quantity.\")\n",
" return inventory\n",
"\n",
"# Previous exercise:\n",
"'''\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
"\n",
" add_another = \"yes\"\n",
"\n",
" while add_another == \"yes\":\n",
" order = input(\"Please input the name of the product: \").lower().strip()\n",
" customer_orders.add(order)\n",
"\n",
" add_another = input(\"Do you want another product? (yes/no): \").lower().strip()\n",
"\n",
" return customer_orders\n",
"'''\n",
"# Now with the error handling\n",
"\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
"\n",
" add_another = \"yes\"\n",
"\n",
" while add_another == \"yes\":\n",
" try:\n",
" order = input(\"Please input the name of the product: \").lower().strip()\n",
"\n",
" if order not in products:\n",
" print(f\"'{order}' is not available. Please choose from: {products}\")\n",
" continue\n",
"\n",
" customer_orders.add(order)\n",
"\n",
" while True:\n",
" add_another = input(\"Do you want another product? (yes/no): \").lower().strip()\n",
"\n",
" if add_another in [\"yes\", \"no\"]:\n",
" break\n",
" else:\n",
" print(\"Please type 'yes' or 'no' only.\")\n",
"\n",
" except ValueError as e:\n",
" print(f\"Error: {e}\")\n",
"\n",
" return customer_orders\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" return {\n",
" product: quantity - 1 if product in customer_orders else quantity\n",
" for product, quantity in inventory.items()\n",
" }\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_order = len(customer_orders)\n",
" rate = (total_order / len(products)) * 100\n",
"\n",
" return total_order, rate\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" total_order, rate = order_statistics\n",
" \n",
" print(f\"Total Ordered: {total_order}\")\n",
" print(f\"Percentagem of products ordered: {rate}\")\n",
"\n",
"\n",
"def print_updated_inventory(inventory):\n",
" [print(f\"{product}: {quantity}\") for product, quantity in inventory.items()]\n",
"\n",
"\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"customer_orders = get_customer_orders()\n",
"\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"\n",
"print_order_statistics(order_statistics)\n",
"\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4f74e438",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +225,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.5"
}
},
"nbformat": 4,
Expand Down