Skip to content

Solved extra lab enia #16

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: master
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
114 changes: 110 additions & 4 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,118 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "cc2c441d-9dcf-4817-b097-cf6cbe440846",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inventory initialized successfully.\n",
"Inventory initialization process completed.\n",
"The product 'book' is out of stock.\n",
"The product 'book' is out of stock.\n",
"Customer orders received successfully.\n",
"Customer order process completed.\n",
"The total price for your orders is: 9.0€\n",
"Price entry process completed.\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"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",
" raise ValueError(\"Quantity cannot be negative.\")\n",
" inventory[product] = quantity\n",
" valid_input = True\n",
" except ValueError as e:\n",
" print(f\"Invalid input: {e}. Please enter a valid quantity.\")\n",
" return inventory\n",
"\n",
"try:\n",
" inventory = initialize_inventory(products)\n",
"except Exception as e:\n",
" print(f\"An error occurred while initializing inventory: {e}\")\n",
"else:\n",
" print(\"Inventory initialized successfully.\")\n",
"finally:\n",
" print(\"Inventory initialization process completed.\")\n",
"\n",
"\n",
"def get_customer_orders(inventory, products):\n",
" customer_orders = set()\n",
" valid_input = False\n",
" while not valid_input:\n",
" try:\n",
" num_ord = int(input(\"Enter the number of products you want to order: \"))\n",
" if num_ord < 0:\n",
" raise ValueError(\"Quantity cannot be negative.\")\n",
" valid_input = True\n",
" except ValueError as e:\n",
" print(f\"Invalid input: {e}. Please enter a valid quantity.\")\n",
" \n",
" for i in range(num_ord):\n",
" valid_order = False\n",
" while not valid_order:\n",
" try:\n",
" order = input(f\"Enter the name of a product from the list: {products} \")\n",
" if order not in inventory:\n",
" raise KeyError(f\"'{order}' is not a valid product.\")\n",
" if inventory[order] == 0:\n",
" raise ValueError(f\"The product '{order}' is out of stock.\")\n",
" customer_orders.add(order)\n",
" valid_order = True\n",
" except KeyError as e:\n",
" print(e)\n",
" except ValueError as e:\n",
" print(e)\n",
" \n",
" return customer_orders\n",
"\n",
"try:\n",
" customer_orders = get_customer_orders(inventory, products)\n",
"except Exception as e:\n",
" print(f\"An error occurred while getting customer orders: {e}\")\n",
"else:\n",
" print(\"Customer orders received successfully.\")\n",
"finally:\n",
" print(\"Customer order process completed.\")\n",
"\n",
"\n",
"def enter_price(customer_orders):\n",
" total_price = 0\n",
" for product in customer_orders:\n",
" while True:\n",
" try:\n",
" price = float(input(f\"Enter the price of the {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",
" \n",
" return f\"{total_price}€\"\n",
"\n",
"try:\n",
" total_prices = enter_price(customer_orders)\n",
"except Exception as e:\n",
" print(f\"An error occurred while entering the prices: {e}\")\n",
"else:\n",
" print(f\"The total price for your orders is: {total_prices}\")\n",
"finally:\n",
" print(\"Price entry process completed.\")\n"
]
}
],
Expand All @@ -66,7 +172,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.10.0"
}
},
"nbformat": 4,
Expand Down