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
257 changes: 254 additions & 3 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,264 @@
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"inventory = {'mug':0, 'hat':0, 'keychain':0, 'book':0, 't-shirt':0}\n",
"\n",
"def initialize_inventory(products):\n",
" for item in inventory:\n",
" products = input (\"Enter a value\")\n",
" if products in inventory:\n",
" value = (int(input(\"Enter a value\")))\n",
" inventory[products]=value\n",
" return products"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "b99b3d7e",
"metadata": {},
"outputs": [],
"source": [
"inventory = {'mug': 0, 'hat': 0, 'keychain': 0, 'book': 0, 't-shirt': 0}\n",
"\n",
"def initialize_inventory(products): \n",
" for products in inventory:\n",
" valid_input = False \n",
" while not valid_input:\n",
" try:\n",
" products = input(\"Enter a product from the available products\")\n",
" if products in inventory:\n",
" valid_input = True\n",
" else: \n",
" print(\"Product not valid\")\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid product\") \n",
" for products in inventory:\n",
" valid_input = False\n",
" while not valid_input:\n",
" try:\n",
" quantity = int(input(f\"Enter the quantity of {products} available: \"))\n",
" if quantity >= 0: \n",
" valid_input = True\n",
" inventory[products] = quantity\n",
" else:\n",
" print(\"Quantity wasn't introduce correctly\")\n",
" except ValueError:\n",
" print(\"Add a valid quantity\")\n",
"\n",
" return inventory\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "510e4b43",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Product not valid\n",
"{'mug': 1, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 5}\n"
]
}
],
"source": [
"products = inventory\n",
"initialize_inventory (products)\n",
"print(products)\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "6b03aff7",
"metadata": {},
"outputs": [],
"source": [
"inventory = {'mug': 1, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 5}\n",
"customer_orders = set()\n",
"\n",
"def get_customers_orders(): \n",
" valid_input = False \n",
" while not valid_input:\n",
" try:\n",
" order_products = int(input(f\"Enter a number of products that you wish to order\")) \n",
" if order_products !=0:\n",
" valid_input = True\n",
" else: \n",
" print(\"Quantity not valid\")\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid quantity\")\n",
" valid_input = False \n",
" while not valid_input:\n",
" for item in range(order_products):\n",
" try:\n",
" order = input(\"Enter a product\")\n",
" if order in inventory: \n",
" valid_input = True\n",
" customer_orders.add(order)\n",
" else:\n",
" print(\"Product isn't available in inventory\")\n",
" except TypeError:\n",
" print(\"Add a valid product. Keep trying\")\n",
"\n",
" return customer_orders\n",
" \n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "f4cf3711",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'mug', 'hat'}\n",
"Thanks for your purchase.\n"
]
}
],
"source": [
"get_customers_orders()\n",
"print(customer_orders)\n",
"print(\"Thanks for your purchase.\")"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "897c2dea",
"metadata": {},
"outputs": [],
"source": [
"inventory = {'mug': 1, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 5}\n",
"customer_orders = ('mug', 't-shirt')\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" for item in customer_orders:\n",
" if inventory[item] >= 0:\n",
" try: \n",
" inventory[item] -= 1\n",
" except KeyError:\n",
" print(\"Error: Product isn't in customer order\") \n",
" else:\n",
" print(\"Operation successful\")\n",
" finally:\n",
" print(\"Operation complete.\")\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "6390c1ba",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Operation successful\n",
"Operation complete.\n",
"Operation successful\n",
"Operation complete.\n"
]
},
{
"data": {
"text/plain": [
"{'mug': 0, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 4}"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"update_inventory(customer_orders, inventory)"
]
},
{
"cell_type": "code",
"execution_count": 87,
"id": "e64ff3a9",
"metadata": {},
"outputs": [],
"source": [
"inventory = {'mug': 1, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 5}\n",
"customer_orders = ('mug', 't-shirt')"
]
},
{
"cell_type": "code",
"execution_count": 88,
"id": "737c6f46",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, inventory):\n",
" for item in customer_orders:\n",
" try: \n",
" total_products_ordered = len(customer_orders)\n",
" total_products_inventory = len(inventory)\n",
" percent_unique_products = (total_products_ordered/total_products_inventory)*100\n",
" if percent_unique_products >= 100:\n",
" print(\"Not enough inventary\")\n",
" else: \n",
" print(\"Operation successful\") \n",
" except ZeroDivisionError:\n",
" print(\"Error: Inventory isn't enough \") \n",
" finally:\n",
" print(\"Operation complete.\")\n",
" return total_products_ordered, percent_unique_products\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 89,
"id": "dfdc3881",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Operation successful\n",
"Operation complete.\n",
"Operation successful\n",
"Operation complete.\n"
]
},
{
"data": {
"text/plain": [
"(2, 40.0)"
]
},
"execution_count": 89,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"calculate_order_statistics(customer_orders, inventory)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -66,7 +317,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down