Skip to content

Solved lab #422

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
371 changes: 370 additions & 1 deletion lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,375 @@
"\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": 3,
"id": "b82ce2e3-6f24-4dd8-854d-78c1f8fd6abd",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: 5\n",
"Enter the quantity of mugs available: 4\n",
"Enter the quantity of hats available: red\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: invalid literal for int() with base 10: 'red'\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of hats available: 6\n",
"Enter the quantity of books available: 15\n",
"Enter the quantity of keychains available: 2\n",
"Enter the number of customer orders, 5\n",
"Enter the name of the product, hat\n",
"Please enter a valid quantity for hat: 6\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"You ordered: 6 of hat\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of the product, hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"'hat' is out of stock. Please choose another product.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of the product, mugs\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Not on inventory,please enter a valid product name.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of the product, t-shirt\n",
"Please enter a valid quantity for t-shirt: 6\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Only 5 are available please enter smaller number.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter a valid quantity for t-shirt: 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"You ordered: 4 of t-shirt\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of the product, hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"'hat' is out of stock. Please choose another product.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of the product, book\n",
"Please enter a valid quantity for book: 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"You ordered: 5 of book\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of the product, mug\n",
"Please enter a valid quantity for mug: 6\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Only 4 are available please enter smaller number.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter a valid quantity for mug: 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Only 4 are available please enter smaller number.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter a valid quantity for mug: 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"You ordered: 2 of mug\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of the product, keychain\n",
"Please enter a valid quantity for keychain: 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Only 2 are available please enter smaller number.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter a valid quantity for keychain: 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"You ordered: 1 of keychain\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the price for hat: 10\n",
"Please enter the price for t-shirt: 15\n",
"Please enter the price for book: 16\n",
"Please enter the price for mug: 5\n",
"Please enter the price for keychain: 100\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total price for all products: 310.00\n"
]
},
{
"data": {
"text/plain": [
"{'hat': 10.0, 't-shirt': 15.0, 'book': 16.0, 'mug': 5.0, 'keychain': 100.0}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Lista de productos\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"\n",
"# Step 1: Define the function for initializing the inventory with error handling\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(\"Invalid quantity! 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",
"# Step 2: Modify calculate_total_price:\n",
"\n",
"def calculate_total_price(customer_orders):\n",
" prices = {}\n",
" for product in customer_orders:\n",
" while True:\n",
" try:\n",
" price = float(input(f\"Please enter the price for {product}: \"))\n",
" if price < 0:\n",
" print(\"Price cannot be negative. Please enter a valid price.\")\n",
" else:\n",
" prices[product] = price\n",
" break\n",
" except ValueError:\n",
" print(f\"Please enter a valid, non-negative number.\")\n",
"\n",
"\n",
" total = sum(prices[product] * quantity for product, quantity in customer_orders.items())\n",
"\n",
" print(f\"Total price for all products: {total:.2f}\")\n",
" return prices\n",
"\n",
" \n",
"# Step 3: \n",
"\n",
"def get_customer_order(inventory):\n",
"\n",
" while True:\n",
" try:\n",
" num_orders = int(input(f\"Enter the number of customer orders, \"))\n",
" if num_orders < 0:\n",
" print(f\"Number of orders cannot be negative. Please try again.\")\n",
" else:\n",
" break\n",
" except ValueError:\n",
" print(f\"Invalid input please enter a valid number.\")\n",
"\n",
" orders = {}\n",
" for i in range(num_orders):\n",
" while True:\n",
" product = input(f\"Enter the name of the product, \").strip().lower()\n",
" if product not in inventory:\n",
" print(f\"Not on inventory,please enter a valid product name.\")\n",
" else:\n",
" if inventory[product] == 0:\n",
" print(f\"'{product}' is out of stock. Please choose another product.\")\n",
" else:\n",
" break\n",
"\n",
" while True:\n",
" try:\n",
" quantity = int(input(f\"Please enter a valid quantity for {product}: \"))\n",
" if quantity < 0:\n",
" print(\"Quantity can not be negative.\")\n",
" else:\n",
" if quantity > inventory[product]:\n",
" print(f\"Only {inventory[product]} are available please enter smaller number.\")\n",
" else:\n",
" if product in orders:\n",
" orders[product] += quantity\n",
" else:\n",
" orders[product] = quantity\n",
" inventory[product] -= quantity\n",
" print(f\"You ordered: {quantity} of {product}\")\n",
" break\n",
" except ValueError:\n",
" print(f\"Invalid input. Please enter a valid quantity.\")\n",
"\n",
" return orders\n",
" \n",
"\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_order(inventory)\n",
"calculate_total_price(customer_orders) \n",
" \n",
" \n",
" \n",
" \n",
"\n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
"\n",
"\n",
" \n",
" \n",
" \n",
" \n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "00e08e80-e4fd-4ecf-a9b3-7640b15aba1f",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "8cea139b-9623-4796-8e79-28dd771565bb",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "5725ba69-8e9e-4fcd-8b02-544d32ab6b0e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -90,7 +459,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.1"
}
},
"nbformat": 4,
Expand Down