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
373 changes: 371 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,380 @@
"\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": 8,
"id": "2cb6bac8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input. invalid literal for int() with base 10: 's'\n",
"Invalid input. Cannot have negative orders. Enter valid number\n",
"Enter the quantity of t-shirt avalable: 8\n",
"Enter the quantity of mug avalable: 8\n",
"Enter the quantity of hat avalable: 8\n",
"Enter the quantity of book avalable: 8\n",
"Enter the quantity of keychain avalable: 8\n",
"Invalid input. invalid literal for int() with base 10: 'f' Enter a valid number.\n",
"Invalid input. Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"Product ordered: mug\n",
"Product ordered: book\n",
"Invalid input. could not convert string to float: 'd' Enter a valid positive number.\n",
"Invalid input. Negative number, that is wrong, add price of good Enter a valid positive number.\n",
"Invalid input. Negative number, that is wrong, add price of good Enter a valid positive number.\n",
"Product Prices:\n",
"mug: €5.0\n",
"book: €40.0\n",
"Total price of customer order: 45.0\n"
]
}
],
"source": [
"#### clean code\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"\n",
"### Setting up inventory count with error handling\n",
"def initialize_inventory(products):\n",
" \"\"\"function for inventory count for each of \n",
" the products in the list and including error handing\"\"\"\n",
" inventory = {}\n",
" for item in products:\n",
" \n",
" while True:\n",
" try:\n",
" quantity = int(input(f\"Please insert inventory count for {item}: \"))\n",
" \n",
" if quantity < 0:\n",
" raise ValueError(f\"Cannot have negative orders. Enter valid number\")\n",
" \n",
" inventory[item] = quantity\n",
" break\n",
"\n",
" except ValueError as ve:\n",
" print(f\"Invalid input. {str(ve)}\")\n",
" \n",
"\n",
" for item in inventory:\n",
" print(f\"Enter the quantity of {item} avalable: {inventory[item]}\")\n",
" \n",
" return inventory\n",
"\n",
"\n",
"#calling the inventory once input recived \n",
"inventory = initialize_inventory(products)\n",
"\n",
"\n",
"\n",
"#setting up error handling to get customer order \n",
"def get_customer_orders(inventory):\n",
" \"\"\"Prompt to get customer order and gather \n",
" product name with error handing\"\"\"\n",
"\n",
" #setting up error handling for initial count of orders to place\n",
" while True:\n",
" try:\n",
"\n",
" num_orders = int(input(\"Enter the number of products customer wants to order: \"))\n",
"\n",
" if num_orders < 0:\n",
" raise ValueError(\"Cannot have negative orders. Enter number of orders\")\n",
" break\n",
"\n",
" except ValueError as ve:\n",
" print(f\"Invalid input. {str(ve)} Enter a valid number.\")\n",
" \n",
" customer_orders = []\n",
" \n",
" #setting up an error handling for selecting from inventory avalable \n",
" while len(customer_orders) < num_orders:\n",
" try:\n",
" product_name = input(f\"Enter product name of the order {len(customer_orders) + 1}:\")\n",
"\n",
" if product_name not in inventory:\n",
" raise ValueError (f\"Product is not available or you spelled the product wrong. Select from the following: {products} \")\n",
"\n",
" customer_orders.append(product_name)\n",
"\n",
" except:\n",
" # Handle invalid product names with an informative message\n",
" print(f\"Invalid input. Available products: {products}\") \n",
"\n",
" for order in customer_orders:\n",
" print(f\"Product ordered: {order}\")\n",
"\n",
"\n",
" return customer_orders\n",
"\n",
"#call for what customer wants to buy\n",
"customer_orders = get_customer_orders(inventory)\n",
"\n",
"\n",
"\n",
"# calculating the price of the customer orders. Add Error Handing here\n",
"def calculate_total_price(customer_orders):\n",
" \"\"\"calculate the price, get input, store in dictionary\n",
" \"\"\"\n",
"\n",
" prices = {}\n",
"\n",
" for product in customer_orders:\n",
"\n",
" while True:\n",
" try:\n",
" input_price = float(input(f\"Enter the price of {product}\"))\n",
"\n",
" if input_price < 0:\n",
" raise ValueError(\"Negative number, that is wrong, add price of good\")\n",
" \n",
" prices[product] = input_price\n",
" break\n",
"\n",
" except ValueError as e:\n",
" print(f\"Invalid input. {str(e)} Enter a valid positive number.\")\n",
"\n",
" total_price = sum(prices.values())\n",
"\n",
" return total_price, prices\n",
"\n",
"\n",
"total_price, product_price = calculate_total_price(customer_orders)\n",
"\n",
"print(\"Product Prices:\")\n",
"for product, price in product_price.items():\n",
" print(f\"{product}: €{price}\")\n",
"\n",
"\n",
"print(f\"Total price of customer order: {total_price}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0c9e00a8",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "e51d6ef4",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "9c03302e",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "81376579",
"metadata": {},
"source": [
"Below is split into sections to solve the Exercise (all copied to the above)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "1defffe6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirt avalable: 4\n",
"Enter the quantity of mug avalable: 4\n",
"Enter the quantity of hat avalable: 4\n",
"Enter the quantity of book avalable: 4\n",
"Enter the quantity of keychain avalable: 4\n"
]
}
],
"source": [
"## 1. Error handiling for the initialize inventory\n",
"\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products):\n",
" \"\"\"function for inventory count for each of \n",
" the products in the list and including error handing\"\"\"\n",
" inventory = {}\n",
" for item in products:\n",
" \n",
" while True:\n",
" try:\n",
" quantity = int(input(f\"Please insert inventory count for {item}: \"))\n",
" \n",
" if quantity < 0:\n",
" raise ValueError(f\"Cannot have negative orders. Enter valid number\")\n",
" \n",
" inventory[item] = quantity\n",
" break\n",
"\n",
" except ValueError as ve:\n",
" print(f\"Invalid input. {str(ve)}\")\n",
" \n",
"\n",
" for item in inventory:\n",
" print(f\"Enter the quantity of {item} avalable: {inventory[item]}\")\n",
" \n",
" return inventory\n",
"\n",
"\n",
"inventory = initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa5b8279",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 6,
"id": "49aa6afe",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input. invalid literal for int() with base 10: 's' Enter a valid number.\n",
"Invalid input. Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"Product ordered: mug\n",
"Product ordered: book\n"
]
}
],
"source": [
"\n",
"#setting up error handling to get customer order \n",
"\n",
"\n",
"def get_customer_orders(inventory):\n",
" \"\"\"Prompt to get customer order and gather \n",
" product name with error handing\"\"\"\n",
"\n",
" #setting up error handling for initial count of orders to place\n",
" while True:\n",
" try:\n",
"\n",
" num_orders = int(input(\"Enter the number of products customer wants to order: \"))\n",
"\n",
" if num_orders < 0:\n",
" raise ValueError(\"Cannot have negative orders. Enter number of orders\")\n",
" break\n",
"\n",
" except ValueError as ve:\n",
" print(f\"Invalid input. {str(ve)} Enter a valid number.\")\n",
" \n",
" customer_orders = []\n",
" \n",
" #setting up an error handling for selecting from inventory avalable \n",
" while len(customer_orders) < num_orders:\n",
" try:\n",
" product_name = input(f\"Enter product name of the order {len(customer_orders) + 1}:\")\n",
"\n",
" if product_name not in inventory:\n",
" raise ValueError (f\"Product is not available or you spelled the product wrong. Select from the following: {products} \")\n",
"\n",
" customer_orders.append(product_name)\n",
"\n",
" except:\n",
" # Handle invalid product names with an informative message\n",
" print(f\"Invalid input. Available products: {products}\") \n",
"\n",
" for order in customer_orders:\n",
" print(f\"Product ordered: {order}\")\n",
"\n",
"\n",
" return customer_orders\n",
"\n",
"#call for what customer wants to buy\n",
"customer_orders = get_customer_orders(inventory)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "b8a521ea",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Product Prices:\n",
"mug: €4.0\n",
"book: €21.0\n",
"Total price of customer order: 25.0\n"
]
}
],
"source": [
"\n",
"#2. calculating the price of the customer orders. Add Error Handing here\n",
"\n",
"\n",
"\n",
"def calculate_total_price(customer_orders):\n",
" \"\"\"calculate the price, get input, store in dictionary\n",
" \"\"\"\n",
"\n",
" prices = {}\n",
"\n",
" for product in customer_orders:\n",
"\n",
" while True:\n",
" try:\n",
" input_price = float(input(f\"Enter the price of {product}\"))\n",
"\n",
" if input_price < 0:\n",
" raise ValueError(\"Negative number, that is wrong, add price of good\")\n",
" \n",
" prices[product] = input_price\n",
" break\n",
"\n",
" except ValueError as e:\n",
" print(f\"Invalid input. {str(e)} Enter a valid positive number.\")\n",
"\n",
" total_price = sum(prices.values())\n",
"\n",
" return total_price, prices\n",
"\n",
"\n",
"total_price, product_price = calculate_total_price(customer_orders)\n",
"\n",
"print(\"Product Prices:\")\n",
"for product, price in product_price.items():\n",
" print(f\"{product}: €{price}\")\n",
"\n",
"\n",
"print(f\"Total price of customer order: {total_price}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "data-analytics",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +459,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.9.23"
}
},
"nbformat": 4,
Expand Down