Skip to content

Add files via upload #36

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: master
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
175 changes: 172 additions & 3 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,182 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "cc2c441d-9dcf-4817-b097-cf6cbe440846",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"# your code goes here\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" while True:\n",
" try:\n",
" quantity = input(f\"Enter the quantity for {product}: \").strip()\n",
" if not quantity.isdigit():\n",
" raise ValueError(f\"{quantity} is not a valid quantity. Please enter a positive integer.\")\n",
" inventory[product] = int(quantity)\n",
" break\n",
" except ValueError as e:\n",
" print(e)\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f84e6933-a2dd-4e1d-866e-751e5b1b8db8",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" customer_orders = set()\n",
" products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
" while True:\n",
" try:\n",
" product = input(\"Enter the name of a product to order (from t-shirt, mug, hat, book, keychain): \").strip().lower()\n",
" if product not in products:\n",
" raise ValueError(f\"{product} is not a valid product. Please choose from the list: {products}\")\n",
" customer_orders.add(product)\n",
" print(f\"{product} has been added to your order.\")\n",
" \n",
" another_product = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n",
" if another_product != \"yes\":\n",
" break\n",
" except ValueError as e:\n",
" print(e)\n",
" return customer_orders\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "bcd6d605-0bc5-47bb-a9f8-079478fc6dca",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" try:\n",
" if product in inventory:\n",
" if inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" else:\n",
" print(f\"{product} not available\")\n",
" else:\n",
" raise KeyError(f\"{product} doesn't exist in this inventory.\")\n",
" except KeyError as e:\n",
" print(e)\n",
" \n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d674dadf-2417-4c96-b7d7-4bf688ae378c",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" try:\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_products_ordered = (total_products_ordered / len(products)) * 100\n",
" return total_products_ordered, percentage_products_ordered\n",
" except ZeroDivisionError:\n",
" print(\"Error: The list of products must not be empty.\")\n",
" return 0, 0\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "3406f45f-e310-4542-ab5d-81d8e829eec8",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" total_products_ordered, percentage_products_ordered = order_statistics\n",
" print(\"Order Statistics:\")\n",
" print(\"Total Products Ordered:\", total_products_ordered)\n",
" print(f\"Percentage of Products Ordered: {percentage_products_ordered:.2f} %\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "d3a9db75-0b97-46d5-8fae-a9d3e2f858b0",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity for t-shirt: 3\n",
"Enter the quantity for mug: 5\n",
"Enter the quantity for hat: 1\n",
"Enter the quantity for book: 0\n",
"Enter the quantity for keychain: 6\n",
"Enter the name of a product to order (from t-shirt, mug, hat, book, keychain): mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"mug has been added to your order.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to add another product? (yes/no): no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 1\n",
"Percentage of Products Ordered: 20.00 %\n",
"Updated inventory:\n",
"t-shirt: 3\n",
"mug: 4\n",
"hat: 1\n",
"book: 0\n",
"keychain: 6\n"
]
}
],
"source": [
"def print_updated_inventory(inventory):\n",
" print(\"Updated inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"# Execution sequence\n",
"try:\n",
" inventory = initialize_inventory(products)\n",
" customer_orders = get_customer_orders()\n",
" inventory = update_inventory(customer_orders, inventory)\n",
" order_statistics = calculate_order_statistics(customer_orders, products)\n",
" print_order_statistics(order_statistics)\n",
" print_updated_inventory(inventory)\n",
"except Exception as e:\n",
" print(f\"An unexpected error occurred: {e}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "778209aa-c1a4-4999-9c4f-fbd84a52c00e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -66,7 +235,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.4"
}
},
"nbformat": 4,
Expand Down