Skip to content

Delivery: lab-python-error-handling-extra #35

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
275 changes: 271 additions & 4 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,285 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 38,
"id": "cc2c441d-9dcf-4817-b097-cf6cbe440846",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"\"\"\" \n",
"1.\n",
"Define a function named initialize_inventory that takes products as a parameter. \n",
"Inside the function, implement the code for initializing the inventory dictionary using a loop and user input.\n",
"\n",
"\"\"\"\n",
"\n",
"\n",
"\n",
"def initialize_inventory(products):\n",
"\n",
" inventory = {}\n",
" \n",
" for product in products:\n",
" \n",
" print(\"Available Products: \", products)\n",
" \n",
" while True:\n",
" \n",
" try:\n",
" \n",
" product_amount = int(input(f\"Enter the amount for {product}: \"))\n",
" \n",
" if product_amount < 0:\n",
" \n",
" raise ValueError(\"Amount cannot be negative.\")\n",
" continue\n",
" \n",
" except ValueError as e:\n",
" print(f\"Error: {e}\")\n",
" continue\n",
" else:\n",
" inventory[product] = product_amount # Only executes if no error\n",
" break\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "acd5bfc3",
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"2.\n",
"\n",
"Define a function named get_customer_orders that takes no parameters. \n",
"Inside the function, implement the code for prompting the user to enter the product names using a loop. \n",
"The function should return the customer_orders set.\n",
"\n",
"\"\"\"\n",
"\n",
"def get_customer_orders(inventory):\n",
" \n",
" customer_orders = set() \n",
" print(\"Available Products: \", inventory)\n",
" \n",
" while True:\n",
"\n",
" order = input(\"Enter your product order name: \").strip().lower()\n",
"\n",
" if order in inventory:\n",
" \n",
" customer_orders.add(order)\n",
" \n",
" else:\n",
" \n",
" print(\"Product not in Inventory\")\n",
" \n",
" ask_customer = input(\"Do you want to add more products? (yes/no): \").strip().lower()\n",
"\n",
" \n",
" if ask_customer == 'no':\n",
" break\n",
" \n",
" return customer_orders\n"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "cbbe9be8",
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"3.\n",
"\n",
"Define a function named update_inventory that takes customer_orders and inventory as parameters. \n",
"Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n",
"\n",
"\"\"\"\n",
"def update_inventory(customer_orders, inventory):\n",
" \n",
" updated_inventory = inventory.copy()\n",
" \n",
" updated_customer_orders_dict = {}\n",
" \n",
" for product in customer_orders:\n",
"\n",
" while True:\n",
" \n",
" try:\n",
" \n",
" quantity = int(input(f\"How many '{product}' would you like to order? \"))\n",
" \n",
" if quantity <= 0:\n",
"\n",
" raise ValueError(\"Quantity must be greater than zero.\")\n",
" \n",
" if quantity > updated_inventory.get(product, 0):\n",
" raise ValueError(f\"Not enough stock for '{product}'. Available: {updated_inventory.get(product, 0)}.\")\n",
" except ValueError as e:\n",
" print(f\"Error: {e}\")\n",
" continue\n",
" else:\n",
" updated_inventory[product] -= quantity\n",
" updated_customer_orders_dict[product] = quantity\n",
" print(\"Order selected:\", updated_customer_orders_dict)\n",
" break # Exit loop when input is valid\n",
"\n",
" return updated_inventory, updated_customer_orders_dict"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "326e2b9f",
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"\n",
"4.\n",
"\n",
"Define a function named calculate_order_statistics that takes customer_orders and products as parameters. \n",
"Inside the function, implement the code for calculating the order statistics (total products ordered, \n",
"and percentage of unique products ordered).\n",
"The function should return these values.\n",
"\n",
"\"\"\"\n",
"def calculate_order_statistics(customer_orders_dict, products):\n",
" \n",
" try:\n",
"\n",
" total_products_ordered = sum(customer_orders_dict.values())\n",
" \n",
" unique_products_ordered = len(customer_orders_dict.keys())\n",
" \n",
" unique_inventory_products = len(products)\n",
" \n",
" percentage = (unique_products_ordered / unique_inventory_products) * 100\n",
"\n",
" except ZeroDivisionError:\n",
" print(\"Error: No products available in inventory.\")\n",
" return 0, 0\n",
"\n",
" return total_products_ordered, percentage\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "8032b18a",
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"\n",
"5.\n",
"\n",
"Define a function named print_order_statistics that takes order_statistics as a parameter. \n",
"Inside the function, implement the code for printing the order statistics.\n",
"\n",
"\"\"\"\n",
"def print_order_statistics(order_statistics):\n",
" \n",
" total_products_ordered, percentage = order_statistics\n",
" \n",
" print(f\"Total products bought: {total_products_ordered}, Percentage of products bought from inventory: {percentage:.2f}%\")\n",
" \n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "bd583bac",
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"\n",
"6.\n",
"\n",
"Define a function named print_updated_inventory that takes inventory as a parameter. \n",
"Inside the function, implement the code for printing the updated inventory.\n",
"\n",
"\"\"\"\n",
"def print_updated_inventory(inventory):\n",
" \n",
" print(\"\\nUpdated Inventory:\")\n",
"\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity} units\")\n"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "cac2bc47",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Available Products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"Available Products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"Available Products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"Available Products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"Available Products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"Available Products: {'t-shirt': 213, 'mug': 241, 'hat': 21, 'book': 124, 'keychain': 123}\n",
"Product not in Inventory\n",
"Product not in Inventory\n",
"Order selected: {'mug': 213}\n",
"Total products bought: 213, Percentage of products bought from inventory: 20.00%\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 213 units\n",
"mug: 28 units\n",
"hat: 21 units\n",
"book: 124 units\n",
"keychain: 123 units\n"
]
}
],
"source": [
"\"\"\"\"\n",
"\n",
"7.\n",
"\n",
"Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"\n",
"\"\"\"\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def main():\n",
" \n",
" \n",
" inventory = initialize_inventory(products)\n",
" customer_orders = get_customer_orders(inventory)\n",
" \n",
" new_inventory, order_dict = update_inventory(customer_orders, inventory)\n",
" \n",
" stats = calculate_order_statistics(order_dict, products)\n",
" \n",
" print_order_statistics(stats)\n",
"\n",
" print_updated_inventory(new_inventory)\n",
"\n",
"\n",
"main()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -66,7 +333,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.3"
}
},
"nbformat": 4,
Expand Down