Skip to content

error handling done #434

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: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
153 changes: 153 additions & 0 deletions .ipynb_checkpoints/lab_error_handling_python-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: Quantity cannot be negative.\n",
"Error: Quantity cannot be negative.\n",
"Enter the product names one by one:\n",
"\n",
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of unique products ordered: 60.0%\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 3\n",
"mug: 1\n",
"hat: 1\n",
"keychain: 3\n",
"\n",
"Total Price: 76.0\n"
]
}
],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" while True:\n",
" try:\n",
" quantity = int(input(f'Enter the quantity of {product}s available: '))\n",
" if quantity < 0:\n",
" raise ValueError('Quantity cannot be negative.')\n",
" inventory[product] = quantity\n",
" break\n",
" except ValueError as e:\n",
" print(f'Error: {e}') \n",
" return inventory\n",
"\n",
"def get_customer_orders(products, inventory):\n",
" while True:\n",
" try:\n",
" num_orders = int(input('Enter the number of customer orders: '))\n",
" if num_orders <= 0:\n",
" raise ValueError('Number of orders must be a positive integer.')\n",
" break \n",
" except ValueError as e:\n",
" print(f'Error: {e}')\n",
" print ('Enter the product names one by one:') \n",
" orders = set()\n",
" i = 0\n",
" while i < num_orders:\n",
" product = input(f'Product {i+1}: ').strip().lower()\n",
" if product not in products:\n",
" print('Invalid product name. Please choose a product from the list.')\n",
" elif inventory.get(product, 0) <= 0:\n",
" print (f'Sorry, {product} is out of stock.')\n",
" else:\n",
" orders.add(product)\n",
" i += 1\n",
" return {'orders': orders} \n",
"\n",
"def calculate_total_price(customer_orders):\n",
" prices = {} \n",
" for item in customer_orders['orders']:\n",
" while True:\n",
" try:\n",
" price = float(input(f'enter the price of {item}: ')) \n",
" if price < 0:\n",
" raise ValueError ('Price cannot be nagative.') \n",
" prices[item] = price\n",
" break \n",
" except ValueError as e:\n",
" print(f'Error: {e}')\n",
" total_price = sum(prices.values())\n",
" return total_price \n",
"\n",
" \n",
"\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders['orders'])\n",
" percentage_ordered = (total_products_ordered * 100) / len(products)\n",
" return total_products_ordered, percentage_ordered\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" print('\\nOrder Statistics:')\n",
" print(f'Total Products Ordered: {order_statistics[0]}')\n",
" print(f'Percentage of unique products ordered: {order_statistics[1]:.1f}%')\n",
"\n",
"def update_inventory(inventory, customer_orders):\n",
" updated_inventory = {\n",
" product: (inventory[product] - 1 if product in customer_orders['orders'] else inventory[product])\n",
" for product in inventory\n",
" }\n",
" filtered_inventory = {product: qty for product, qty in updated_inventory.items() if qty > 0}\n",
" return filtered_inventory\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print('\\nUpdated Inventory:')\n",
" for product, qty in inventory.items():\n",
" print(f'{product}: {qty}')\n",
"\n",
"\n",
"\n",
"def main():\n",
" products = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
" inventory = initialize_inventory(products)\n",
" customer_orders = get_customer_orders(products, inventory)\n",
"\n",
" order_statistics = calculate_order_statistics(customer_orders, products)\n",
" print_order_statistics(order_statistics)\n",
"\n",
" inventory = update_inventory(inventory, customer_orders)\n",
" print_updated_inventory(inventory)\n",
"\n",
" total_price = calculate_total_price(customer_orders)\n",
" print(f'\\nTotal Price: {total_price:.1f}')\n",
"\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Binary file added anaconda_projects/db/project_filebrowser.db
Binary file not shown.
156 changes: 156 additions & 0 deletions lab_error_handling1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: Quantity cannot be negative.\n",
"Error: Quantity cannot be negative.\n",
"Enter the product names one by one:\n",
"Invalid product name. Please choose a product from the list.\n",
"Invalid product name. Please choose a product from the list.\n",
"\n",
"Order Statistics:\n",
"Total Products Ordered: 5\n",
"Percentage of unique products ordered: 100.0%\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 1\n",
"hat: 1\n",
"book: 1\n",
"keychain: 1\n",
"\n",
"Total Price: 83.0\n"
]
}
],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" while True:\n",
" try:\n",
" quantity = int(input(f'Enter the quantity of {product}s available: '))\n",
" if quantity < 0:\n",
" raise ValueError('Quantity cannot be negative.')\n",
" inventory[product] = quantity\n",
" break\n",
" except ValueError as e:\n",
" print(f'Error: {e}') \n",
" return inventory\n",
"\n",
"def get_customer_orders(products, inventory):\n",
" while True:\n",
" try:\n",
" num_orders = int(input('Enter the number of customer orders: '))\n",
" if num_orders <= 0:\n",
" raise ValueError('Number of orders must be a positive integer.')\n",
" break \n",
" except ValueError as e:\n",
" print(f'Error: {e}')\n",
" print ('Enter the product names one by one:') \n",
" orders = set()\n",
" i = 0\n",
" while i < num_orders:\n",
" product = input(f'Product {i+1}: ').strip().lower()\n",
" if product not in products:\n",
" print('Invalid product name. Please choose a product from the list.')\n",
" elif inventory.get(product, 0) <= 0:\n",
" print (f'Sorry, {product} is out of stock.')\n",
" else:\n",
" orders.add(product)\n",
" i += 1\n",
" return {'orders': orders} \n",
"\n",
"def calculate_total_price(customer_orders):\n",
" prices = {} \n",
" for item in customer_orders['orders']:\n",
" while True:\n",
" try:\n",
" price = float(input(f'enter the price of {item}: ')) \n",
" if price < 0:\n",
" raise ValueError ('Price cannot be nagative.') \n",
" prices[item] = price\n",
" break \n",
" except ValueError as e:\n",
" print(f'Error: {e}')\n",
" total_price = sum(prices.values())\n",
" return total_price \n",
"\n",
" \n",
"\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders['orders'])\n",
" percentage_ordered = (total_products_ordered * 100) / len(products)\n",
" return total_products_ordered, percentage_ordered\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" print('\\nOrder Statistics:')\n",
" print(f'Total Products Ordered: {order_statistics[0]}')\n",
" print(f'Percentage of unique products ordered: {order_statistics[1]:.1f}%')\n",
"\n",
"def update_inventory(inventory, customer_orders):\n",
" updated_inventory = {\n",
" product: (inventory[product] - 1 if product in customer_orders['orders'] else inventory[product])\n",
" for product in inventory\n",
" }\n",
" filtered_inventory = {product: qty for product, qty in updated_inventory.items() if qty > 0}\n",
" return filtered_inventory\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print('\\nUpdated Inventory:')\n",
" for product, qty in inventory.items():\n",
" print(f'{product}: {qty}')\n",
"\n",
"\n",
"\n",
"def main():\n",
" products = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
" inventory = initialize_inventory(products)\n",
" customer_orders = get_customer_orders(products, inventory)\n",
"\n",
" order_statistics = calculate_order_statistics(customer_orders, products)\n",
" print_order_statistics(order_statistics)\n",
"\n",
" inventory = update_inventory(inventory, customer_orders)\n",
" print_updated_inventory(inventory)\n",
"\n",
" total_price = calculate_total_price(customer_orders)\n",
" print(f'\\nTotal Price: {total_price:.1f}')\n",
"\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()\n",
"#"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading