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
201 changes: 199 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,208 @@
"\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": 194,
"id": "0d6a6b6b",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 195,
"id": "d3e656b0",
"metadata": {},
"outputs": [],
"source": [
"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"
]
},
{
"cell_type": "code",
"execution_count": 212,
"id": "524380d3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 12, 'mug': 123, 'hat': 11, 'book': 22, 'keychain': 1}"
]
},
"execution_count": 212,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 213,
"id": "8e66a6b8",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders(inventory):\n",
" while True:\n",
" try:\n",
" order_count = int(input(\"How many products do you want? \"))\n",
" if order_count < 0:\n",
" print(\"Please enter a positive number.\")\n",
" continue\n",
" break\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a numeric value for the quantity.\")\n",
"\n",
" customer_orders = set()\n",
"\n",
" for i in range(order_count):\n",
" while True:\n",
" product_name = input(f\"Enter the name of product please: \")\n",
" \n",
" if product_name not in inventory:\n",
" print(f\"Sorry, '{product_name}' is not in our inventory. Try again.\")\n",
" elif inventory[product_name] <= 0:\n",
" print(f\"Sorry, '{product_name}' is out of stock. Please choose another.\")\n",
" else:\n",
" customer_orders.add(product_name)\n",
" break\n",
" \n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 214,
"id": "23a07f1b",
"metadata": {},
"outputs": [],
"source": [
"inventory = {'t-shirt': 12, 'mug': 123, 'hat': 11, 'book': 22, 'keychain': 1}"
]
},
{
"cell_type": "code",
"execution_count": 215,
"id": "96d3df6b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter a positive number.\n",
"Invalid input. Please enter a numeric value for the quantity.\n",
"Invalid input. Please enter a numeric value for the quantity.\n",
"Sorry, 'hats' is not in our inventory. Try again.\n",
"Sorry, '2' is not in our inventory. Try again.\n"
]
},
{
"data": {
"text/plain": [
"{'book', 'hat', 'mug'}"
]
},
"execution_count": 215,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_customer_orders(inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": 219,
"id": "ec062702",
"metadata": {},
"outputs": [],
"source": [
"def total_price(costumer_orders):\n",
" cost_of_products = []\n",
"\n",
" for item in costumer_orders:\n",
" valid_price = False \n",
" while not valid_price:\n",
" try: \n",
" price = float(input(f\"Enter a valid price of {item}\")) \n",
" if price < 0: \n",
" raise ValueError(f\"Your {item} must have a positive price\")\n",
" cost_of_products.append(price)\n",
" valid_price = True\n",
" except ValueError as error: \n",
" print(f\"Error: {error}\")\n",
"\n",
" result = sum(cost_of_products)\n",
" return result\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 217,
"id": "a5a76821",
"metadata": {},
"outputs": [],
"source": [
"costumer_orders = [\"mug\", \"hat\", \"book\"]"
]
},
{
"cell_type": "code",
"execution_count": 218,
"id": "aa071b4e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: Your mug must have a positive price\n",
"Error: could not convert string to float: 'ha'\n"
]
},
{
"data": {
"text/plain": [
"33.5"
]
},
"execution_count": 218,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"total_price(costumer_orders)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "ds",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +287,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.12"
}
},
"nbformat": 4,
Expand Down