Skip to content
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

week 1 lab 3 done #299

Open
wants to merge 1 commit into
base: main
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
208 changes: 207 additions & 1 deletion lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,212 @@
"\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": 15,
"id": "fda072d9-0af4-4662-94e5-d7e059fed332",
"metadata": {},
"outputs": [],
"source": [
"#Modify the calculate_total_price function to include error handling.\n",
"#If the user enters an invalid price (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the price for that product.\n",
"#Use a try-except block to handle the error and continue prompting the user until a valid price is entered.\n",
"products = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" valid_quantity = False\n",
" while not valid_quantity:\n",
" try:\n",
" quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n",
" if quantity < 0:\n",
" raise ValueError(\"Please enter a non-negative value.\")\n",
" valid_quantity = True\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
" inventory[product] = quantity\n",
" return inventory\n",
"\n",
"def get_customer_orders(products, inventory):\n",
" while True:\n",
" try:\n",
" num_orders = int(input(\"How many products does the customer want to order? \"))\n",
" if num_orders < 0:\n",
" raise ValueError(\"The number of orders cannot be negative.\")\n",
" break\n",
" except ValueError as e:\n",
" print(f\"Invalid input: {e}. Please enter a positive integer for the number of orders.\")\n",
"\n",
" customer_orders = set()\n",
" for i in range(num_orders):\n",
" while True:\n",
" product = input(f\"Enter the name of product #{i+1} from the following list {products}: \")\n",
" if product in products and inventory.get(product, 0) > 0:\n",
" customer_orders.add(product)\n",
" break\n",
" else:\n",
" print(\"Invalid product name. \")\n",
"\n",
" return customer_orders\n",
"\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" updated_inventory = {product: count - (1 if product in customer_orders else 0)for product, count in inventory.items()}\n",
"\n",
" updated_inventory = {product: count for product, count in updated_inventory.items() if count > 0}\n",
" \n",
" return updated_inventory\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" unique_products = set(products)\n",
" percentage = (total_products_ordered / len(unique_products)) * 100\n",
" return total_products_ordered, percentage\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" total_products_ordered, percentage_ordered = order_statistics\n",
" print(\"Order Statistics:\")\n",
" print(f\"Total Products Ordered: {total_products_ordered}\")\n",
" print(f\"Percentage of Products Ordered: {percentage_ordered}%\")\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\") \n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"\n",
"def calculate_total_price(customer_orders):\n",
" total_price = 0.0\n",
" for product in customer_orders:\n",
" while True:\n",
" try:\n",
" price = float(input(f\"Enter the price for {product}: \"))\n",
" if price < 0:\n",
" raise ValueError(\"Price cannot be negative.\")\n",
" total_price += price\n",
" break\n",
" except ValueError as e:\n",
" print(f\"Invalid input: {e}. Please enter a valid price.\")\n",
" return total_price"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "362d27e1-7ed2-4b57-8e45-5db51df1080f",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: -5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: Please enter a non-negative value.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: 5\n",
"Enter the quantity of mugs available: 5\n",
"Enter the quantity of hats available: 5\n",
"Enter the quantity of books available: 5\n",
"Enter the quantity of keychains available: 5\n",
"How many products does the customer want to order? -10\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input: The number of orders cannot be negative.. Please enter a positive integer for the number of orders.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"How many products does the customer want to order? 2\n",
"Enter the name of product #1 from the following list ['t-shirt', 'mug', 'hat', 'book', 'keychain']: mug\n",
"Enter the name of product #2 from the following list ['t-shirt', 'mug', 'hat', 'book', 'keychain']: hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 40.0%\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 5\n",
"mug: 4\n",
"hat: 4\n",
"book: 5\n",
"keychain: 5\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price for mug: -3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input: Price cannot be negative.. Please enter a valid price.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price for mug: 15\n",
"Enter the price for hat: 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Price: 17.0\n"
]
}
],
"source": [
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders(products, inventory)\n",
"print()\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(order_statistics)\n",
"print()\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"print_updated_inventory(inventory)\n",
"total_price = calculate_total_price(customer_orders)\n",
"print(f\"Total Price: {total_price:.1f}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cd9d40e7-d042-421a-a78c-6a387fd93b41",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -90,7 +296,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down