Skip to content

Solved lab error handling #427

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
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
294 changes: 293 additions & 1 deletion lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,298 @@
"\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": 1,
"id": "d21413ba-093e-442e-999d-404ab4c9377b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['t-shirt', 'mug', 'hat', 'book', 'keychain']"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"products"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "7a83244d-b387-40e7-8e35-a78fb539ce81",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: \n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input. Please enter a valid quantity.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: 100\n",
"Enter the quantity of mugs available: 100\n",
"Enter the quantity of hats available: 100\n",
"Enter the quantity of books available: 100\n",
"Enter the quantity of keychains available: 100\n"
]
}
],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" valid_input False\n",
" while not valid_input:\n",
" try:\n",
" quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n",
" if quantity >= 0:\n",
" inventory[product] = quantity\n",
" valid_input = True\n",
" else:= \n",
" print(\"Quantity cannot be negative. Please enter a valid quantity.\")\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid quantity.\")\n",
" return inventory\n",
"\n",
"inventory = initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9b25435a-381c-46cf-b500-e137e3b09aba",
"metadata": {},
"outputs": [],
"source": [
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "57715420-cab7-49b8-9ce9-9bd086edf6a5",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Insert how many products do you want to order: 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order 1:\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of the product to order: mug\n",
"Enter the quantity of mug: 20\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order 2:\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of the product to order: hat\n",
"Enter the quantity of hat: 50\n"
]
}
],
"source": [
"def get_customer_orders(inventory):\n",
" customer_orders = {}\n",
" while True:\n",
" try:\n",
" order_count = int(input(\"Insert how many products do you want to order: \"))\n",
" if order_count <= 0:\n",
" print(\"Please enter a positive number.\")\n",
" else:\n",
" break\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a numeric value.\")\n",
"\n",
" for i in range(order_count):\n",
" print(f\"Order {i + 1}:\")\n",
" while True:\n",
" product = input(\"Enter the name of the product to order: \")\n",
" if product not in inventory:\n",
" print(\"Invalid product name.\")\n",
" elif inventory[product] <= 0:\n",
" print(\"Sorry, this product is out of stock.\")\n",
" else:\n",
" break\n",
"\n",
" while True:\n",
" try:\n",
" quantity = int(input(f\"Enter the quantity of {product}: \"))\n",
" if quantity < 0:\n",
" print(\"Quantity cannot be negative.\")\n",
" elif quantity > inventory[product]:\n",
" print(\"Insufficient stock.\")\n",
" else:\n",
" customer_orders[product] = quantity\n",
" break\n",
" except ValueError:\n",
" print(\"Invald input.\")\n",
"\n",
" return customer_orders\n",
"\n",
"\n",
"customer_orders = get_customer_orders(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "fc3c3901-205f-4354-aab5-a0f7b2e6c2e3",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Insert the price of mug: 6\n",
"Insert the price of hat: 10\n"
]
}
],
"source": [
"def calculate_total_price(customer_orders):\n",
" total_price = {}\n",
" for product, quantity in customer_orders.items():\n",
" valid_input = False\n",
" while not valid_input:\n",
" try:\n",
" price = int(input(f\"Insert the price of {product}: \"))\n",
" if price >= 0:\n",
" total_price[product] = price\n",
" valid_input = True\n",
" else:\n",
" print(\"Price cannot be negative. Please enter a valid price.\")\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid price.\")\n",
" return total_price\n",
"\n",
"total_price = calculate_total_price(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "099315b7-5e02-4e98-b9b5-959f583c9669",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The total of price of the customer order is: 16\n"
]
}
],
"source": [
"def total_price_customer_order(total_price):\n",
" print(f\"The total of price of the customer order is: {sum(total_price.values())}\")\n",
"\n",
"total_price_customer_order(total_price)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "2210754c-d73d-4df2-8f8a-258fbf00c946",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" sum_order_prod = sum(qty for _, qty in customer_orders)\n",
" sum_invent_prod = sum(inventory.values())\n",
" print(f\"Total of products ordered: {sum_order_prod}\")\n",
" percentage = (sum_order_prod / sum_invent_prod) * 100\n",
" print(f\"Percentage of products ordered: {percentage:.2f}%\")\n",
" return (sum_order_prod, percentage)\n"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "202bebf8-99f9-4017-b25b-9ebb7045691e",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" sum_order_prod, percentage = order_statistics\n",
" print({sum_order_prod} , {percentage})"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "f97fd70c-3716-44cb-89af-d2fa560b961d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 100, 'mug': 80, 'hat': 50, 'book': 100, 'keychain': 100}\n"
]
}
],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" for product, quantity_order in customer_orders.items():\n",
" if product in inventory:\n",
" inventory[product] -= quantity_order\n",
" if inventory[product] < 0:\n",
" inventory[product] = 0\n",
"\n",
" return inventory\n",
"\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ecf19b90-6838-43f2-9f63-0a919f5459d1",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -90,7 +382,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down