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
235 changes: 232 additions & 3 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,242 @@
"\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": "d1addfa9-8bdb-401b-a6f3-9f574caf6a9c",
"metadata": {},
"outputs": [],
"source": [
"customer_order = ['mug', 'hat', 'book']\n",
"\n",
"def calculate_total_price (): \n",
" price_products = { item : float(input(\"Please introduce the price of the product\")) for item in customer_order }\n",
" return f\"Total price:{sum (price_products.values())}\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "94409b7c-b45d-4f5b-a8f1-03841a5446e0",
"metadata": {},
"outputs": [],
"source": [
"calculate_total_price ()\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9d364330-2eb0-4a96-8189-41c2959a6b4a",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "6449500f-ce5d-40d2-bc90-c1bd808d58bb",
"metadata": {},
"outputs": [],
"source": [
"def calculate_total_price():\n",
" \n",
" price_products = {}\n",
" for item in customer_order:\n",
" while True:\n",
" try:\n",
" price = float(input(\"Please introduce the price of the product\")) \n",
" price_products[item] = price\n",
" break \n",
" except ValueError:\n",
" print (\"Invalid input. Please enter a valid quantity.\")\n",
" return f\"Total price:{sum (price_products.values())}\"\n",
"\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1702c9e2-94ce-42a2-923e-e354e455c777",
"metadata": {},
"outputs": [],
"source": [
"calculate_total_price()"
]
},
{
"cell_type": "markdown",
"id": "1360c056-f3c5-484b-8f96-8cdce38e122c",
"metadata": {},
"source": [
"4. If the user enters an invalid number of orders (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the number of orders.\n",
"5. If the user enters an invalid product name (e.g., a product name that is not in the inventory), or that doesn't have stock available, display an error message and ask them to re-enter the product name. Hint: you will need to pass inventory as a parameter\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "bab79c2f-1d63-4a3b-84f2-9a49ca8fd2b5",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders(): \n",
" customer_orders = []\n",
" while True: \n",
" try: \n",
" number_orders = int(input(\"Please enter the number of products you want to order\")) \n",
" if number_orders < 0: \n",
" print (\"Please enter a positive number\") \n",
" break \n",
" \n",
" except ValueError: \n",
" print (\"Invalid input. Please enter a valid number.\")\n",
"\n",
" for item in range (number_orders): \n",
" orders = input(\"Please enter the product name\")\n",
" customer_orders.append(orders) \n",
" \n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f65ec0b7-e12b-47df-b95d-9d0fa9305836",
"metadata": {},
"outputs": [],
"source": [
"inventory = {'t-shirt': 1, 'mug': 0, 'hat': 1, 'book': 2, 'keychain': 4}"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "e614ff97-c6f8-4d60-bc2c-03062b3f4989",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders(inventory): \n",
" customer_orders = []\n",
" while True: \n",
" try: \n",
" number_orders = int(input(\"Please enter the number of products you want to order\")) \n",
" if number_orders < 0: \n",
" print (\"Please enter a positive number\") \n",
" break \n",
" \n",
" except ValueError: \n",
" print (\"Invalid input. Please enter a valid number.\")\n",
"\n",
" for item in range (number_orders): \n",
" while True: \n",
" orders = input(\"Please enter the product name\")\n",
" if orders not in inventory: \n",
" print (\"Product does not exist\") \n",
" continue \n",
" if inventory[orders] <= 0: \n",
" print (\"Product is out of stock\") \n",
" continue\n",
" customer_orders.append(orders) \n",
" break \n",
" \n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "127a3fe8-87b0-49a7-9e6c-be056e1714ab",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the number of products you want to order 3\n",
"Please enter the product name mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Product is out of stock\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the product name book\n",
"Please enter the product name hat\n",
"Please enter the product name mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Product is out of stock\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the product name keychain\n"
]
},
{
"data": {
"text/plain": [
"['book', 'hat', 'keychain']"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_customer_orders(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f8b23fd6-805e-488f-96d2-ba6c406f459f",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "8359f956-57da-437a-802a-5a3a5cb485a2",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "1dcd0621-bcb7-46b5-a4e7-52562dd25b8e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -90,7 +319,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down