Skip to content

solve lab #439

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
Show file tree
Hide file tree
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
340 changes: 340 additions & 0 deletions .ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,340 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e",
"metadata": {},
"source": [
"# Lab | Error Handling"
]
},
{
"cell_type": "markdown",
"id": "bc99b386-7508-47a0-bcdb-d969deaf6c8b",
"metadata": {},
"source": [
"## Exercise: Error Handling for Managing Customer Orders\n",
"\n",
"The implementation of your code for managing customer orders assumes that the user will always enter a valid input. \n",
"\n",
"For example, we could modify the `initialize_inventory` function to include error handling.\n",
" - If the user enters an invalid quantity (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the quantity for that product.\n",
" - Use a try-except block to handle the error and continue prompting the user until a valid quantity is entered.\n",
"\n",
"```python\n",
"# Step 1: Define the function for initializing the inventory with error handling\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",
"# Or, in another way:\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",
" inventory[product] = quantity\n",
" valid_input = True\n",
" else:\n",
" print(\"Quantity cannot be negative. Please enter a valid quantity.\")\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid quantity.\")\n",
" return inventory\n",
"```\n",
"\n",
"Let's enhance your code by implementing error handling to handle invalid inputs.\n",
"\n",
"Follow the steps below to complete the exercise:\n",
"\n",
"2. Modify the `calculate_total_price` function to include error handling.\n",
" - If the user enters an invalid price (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the price for that product.\n",
" - Use a try-except block to handle the error and continue prompting the user until a valid price is entered.\n",
"\n",
"3. Modify the `get_customer_orders` function to include error handling.\n",
" - 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",
" - 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",
" - Use a try-except block to handle the error and continue prompting the user until a valid product name is entered.\n",
"\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": 5,
"id": "6a54c6f8-ab89-45df-bcb8-2ab26a4442b6",
"metadata": {},
"outputs": [],
"source": [
"# 2\n",
"def calculate_total_price(customer_order):\n",
" total = []\n",
" for product in customer_order:\n",
" valid_input = False\n",
" while not valid_input:\n",
" try:\n",
" price = float(input(f\"Enter the price of {product}\"))\n",
" if price > 0:\n",
" total.append(price)\n",
" valid_input = True\n",
" else:\n",
" print(\"Price cannot be zero or negative. Please enter a valid price.\")\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid price\")\n",
" return round(sum(total), 2)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "4cbf887e-8b5e-47a2-9cce-394e16451cda",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price of book 2.76\n",
"Enter the price of mug no price\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input. Please enter a valid price\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price of mug 0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Price cannot be zero or negative. Please enter a valid price.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price of mug -4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Price cannot be zero or negative. Please enter a valid price.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price of mug 8\n",
"Enter the price of keychain 10\n"
]
},
{
"data": {
"text/plain": [
"20.76"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 4 Test function\n",
"calculate_total_price([\"book\", \"mug\", \"keychain\"])"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "d8016be0-bc85-4654-9b06-c47f3767d4f6",
"metadata": {},
"outputs": [],
"source": [
"# 3\n",
"def get_customer_orders(inventory):\n",
" valid_input = False\n",
" while not valid_input:\n",
" try:\n",
" qty = int((input(\"Enter the number of customer orders: \")))\n",
" if qty > 0:\n",
" valid_input = True\n",
" else:\n",
" print(\"Number of customer orders cannot be zero or negative. Please enter a valid price.\")\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a number of customer orders\")\n",
" customer_orders = set()\n",
" for i in range(qty):\n",
" valid_input = False\n",
" while not valid_input:\n",
" order = input(\"Enter the name of a product that a customer wants to order:\")\n",
" if order.lower() not in [word.lower() for word in inventory.keys()]:\n",
" print(f\"The product is not in the inventory. Products available are: {[product for product in inventory.keys()]}\")\n",
" elif inventory[order] <= 0:\n",
" print(f\"The product is not in stock! Our current stock is: {inventory}\")\n",
" else:\n",
" customer_orders.add(order)\n",
" valid_input = True\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "7494852b-1b10-4631-818a-540621731c44",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the number of customer orders: no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input. Please enter a number of customer orders\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the number of customer orders: 0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of customer orders cannot be zero or negative. Please enter a valid price.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the number of customer orders: -3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of customer orders cannot be zero or negative. Please enter a valid price.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the number of customer orders: 3\n",
"Enter the name of a product that a customer wants to order: trying\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The product is not in the inventory. Products available are: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of a product that a customer wants to order: hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The product is not in stock! Our current stock is: {'t-shirt': 4, 'mug': 7, 'hat': 0, 'book': 2, 'keychain': 9}\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of a product that a customer wants to order: t-shirt\n",
"Enter the name of a product that a customer wants to order: mug\n",
"Enter the name of a product that a customer wants to order: keychain\n"
]
},
{
"data": {
"text/plain": [
"{'keychain', 'mug', 't-shirt'}"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 4 Test function\n",
"inventory = {\n",
" \"t-shirt\":4,\n",
" \"mug\":7,\n",
" \"hat\":0,\n",
" \"book\":2,\n",
" \"keychain\":9\n",
"}\n",
"\n",
"get_customer_orders(inventory)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading