Skip to content

lab pyton error handling completed #415

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
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
176 changes: 174 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,183 @@
"\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": 10,
"id": "f7afecde",
"metadata": {},
"outputs": [],
"source": [
"products = ['book', 'mug', 'hat']\n",
"\n",
"inventory = {\n",
" 'book': 10,\n",
" 'mug': 20,\n",
" 'hat': 0\n",
"}\n",
"\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"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "6b934843",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"bad input invalid literal for int() with base 10: 'p'\n",
"bad input Invalid price! Please enter a non-negative value.\n"
]
},
{
"data": {
"text/plain": [
"14"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def calculate_price (customer_orders):\n",
" price_list = []\n",
" for product in customer_orders:\n",
" valid_input = False\n",
" while not valid_input:\n",
" try:\n",
" price = int(input(f'How much is the {product}?'))\n",
" if price <= 0:\n",
" raise ValueError(\"Invalid price! Please enter a non-negative value.\")\n",
" valid_input = True\n",
" except ValueError as error:\n",
" print ('bad input', error)\n",
" price_list.append(price) \n",
" return sum(price_list)\n",
"\n",
"calculate_price({'book', 'mug'})\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "2be770ad",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You made a mistake: hat is out of stock\n",
"You made a mistake: There is no such a key\n"
]
},
{
"data": {
"text/plain": [
"{'book', 'mug'}"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def get_customer_orders(inventory):\n",
" result = set()\n",
" valid_unit = False\n",
" while not valid_unit:\n",
" try:\n",
" unit = int(input('Enter the number of customer orders: '))\n",
" if unit < 0:\n",
" raise ValueError(\"Invalid unit! Please enter a non-negative value.\")\n",
" valid_unit = True\n",
" except ValueError as error:\n",
" print ('bad input', error)\n",
"\n",
" for i in range(int(unit)):\n",
" \n",
" valid_product = False\n",
" while not valid_product:\n",
" try:\n",
" product = input('Enter the name of a product that a customer wants to order: ')\n",
" if product not in inventory:\n",
" raise ValueError(f\"There is no such a {product}\")\n",
" elif inventory[product] == 0:\n",
" raise ValueError(f\"{product} is out of stock\")\n",
" else:\n",
" valid_product = True\n",
" result.add(product)\n",
" except ValueError as error:\n",
" print('You made a mistake:', error)\n",
" \n",
" return (result)\n",
"\n",
"get_customer_orders(inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cf11437f",
"metadata": {},
"outputs": [],
"source": [
"## Version 2:\n",
"\n",
"def get_customer_orders2(inventory):\n",
" result = set()\n",
" while True:\n",
" try:\n",
" unit = int(input('Enter the number of customer orders: '))\n",
" if unit < 0:\n",
" raise ValueError(\"Invalid unit! Please enter a non-negative value.\")\n",
" else:\n",
" break\n",
" except ValueError as error:\n",
" print ('bad input', error)\n",
"\n",
" for i in range(int(unit)):\n",
" while True:\n",
" try:\n",
" product = input('Enter the name of a product that a customer wants to order: ')\n",
" if product not in inventory:\n",
" raise ValueError(f\"There is no such a {product}\")\n",
" elif inventory[product] == 0:\n",
" raise ValueError(f\"{product} is out of stock\")\n",
" else:\n",
" result.add(product)\n",
" break\n",
" except ValueError as error:\n",
" print('You made a mistake:', error)\n",
" \n",
" return (result)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +262,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down