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
275 changes: 274 additions & 1 deletion lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,279 @@
"\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": null,
"id": "da597fad-a0d3-49b5-9e55-951efa7b25ed",
"metadata": {},
"outputs": [],
"source": [
"######\n",
"products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"def initialize_inventory(products):\n",
" inventory={product: int(input(f\"quantity of {product}:\")) for product in products}\n",
" return inventory\n",
"#initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "8465b3be-559e-4ee1-9322-6c550dd66de8",
"metadata": {},
"outputs": [],
"source": [
"#1\n",
"products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\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",
"#initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c63957b1-80e5-4d2b-ad48-609ae4e67b89",
"metadata": {},
"outputs": [],
"source": [
"#2\n",
"def get_customer_orders():\n",
" customer_orders=set()\n",
" NumberOrders = int(input(f\"number of orders:\"))\n",
" while NumberOrders <= 0:\n",
" print( f\"enter a correct order number\")\n",
" NumberOrders = int(input(f\"number of orders:\"))\n",
" while NumberOrders>0:\n",
" customer_product=input(f\"add a product from the list {products}:\") \n",
" customer_orders.update({customer_product} if customer_product in products else {})\n",
" NumberOrders -= 1 if customer_product in products else 0\n",
" return customer_orders\n",
"#get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "de663730-e768-42af-87d7-9487ff84386b",
"metadata": {},
"outputs": [],
"source": [
"#d"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "0813dc4f-ebc6-48ff-b5c6-79bfdb6154cc",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: 5\n",
"Enter the quantity of mugs available: 5\n",
"Enter the quantity of hats available: 5\n",
"Enter the quantity of books available: 5\n",
"Enter the quantity of keychains available: 5\n",
"number of orders: 2\n",
"add a product from the list ['t-shirt', 'mug', 'hat', 'book', 'keychain']: book\n",
"add a product from the list ['t-shirt', 'mug', 'hat', 'book', 'keychain']: hat\n",
"enter a price for book: 5\n",
"enter a price for hat: '2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"invalid input\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"enter a price for hat: '1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"invalid input\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"enter a price for hat: 1\n"
]
},
{
"data": {
"text/plain": [
"'total price is 6'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#3\n",
"inventory=initialize_inventory(products)\n",
"customer_orders=get_customer_orders()\n",
"def get_customer_orders_price(customer_orders):\n",
" CustomerPrices=[]\n",
" for product in customer_orders:\n",
" while True: \n",
" try:\n",
" price_str = input(f\"enter a price for {product}:\")\n",
" price= int(price_str)\n",
" if price <= 0:\n",
" raise ValueError (\"number must be positive \")\n",
" CustomerPrices.append(price)\n",
" break\n",
" except:\n",
" print(\"invalid input\")\n",
" return f\"total price is {sum(CustomerPrices)}\"\n",
"get_customer_orders_price(customer_orders)"
]
},
{
"cell_type": "markdown",
"id": "4d291cba-f559-440c-a6c6-22735fc3bcfe",
"metadata": {},
"source": [
"##### "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1183fe39-74bf-4714-90d4-fc9c1ebc41b2",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"quantity of t-shirt: 5\n",
"quantity of mug: 5\n",
"quantity of hat: 5\n",
"quantity of book: 5\n",
"quantity of keychain: 5\n",
"number of orders: 2\n"
]
}
],
"source": [
"#4\n",
"inventory=initialize_inventory(products)\n",
"customer_orders=get_customer_orders()\n",
"def update_inventory(customer_orders, inventory):\n",
" CustOrderList=list(customer_orders)\n",
" inventory = {order:inventory[order]-CustOrderList.count(order) for order in inventory if inventory[order]-CustOrderList.count(order)!=0}\n",
" return print(f\"updated inventory: {inventory}\")\n",
" \n",
"#update_inventory(customer_orders, inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e32ee759-ef3c-4488-b7c2-8d636ab7b8db",
"metadata": {},
"outputs": [],
"source": [
"#5\n",
"def calculate_order_statistics(customer_orders, products):\n",
" number_orders=len(customer_orders)\n",
" ordered_products=f\"{int((number_orders/len(products))*100)}%\"\n",
" return number_orders, ordered_products\n",
"\n",
"#calculate_order_statistics(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bc27ddd3-91b9-4059-90ce-064cb13415ab",
"metadata": {},
"outputs": [],
"source": [
"#6\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"def print_order_statistics(order_statistics):\n",
" print(f\"number of products:\", order_statistics[0], \"products %:\", order_statistics[1])\n",
"\n",
"#print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "16d68e8f-8776-4714-b15a-d18a9ae0560d",
"metadata": {},
"outputs": [],
"source": [
"#7\n",
"def print_updated_inventory(inventory):\n",
" return print(inventory)\n",
"#print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aee5f95e-9f3d-473a-965f-cac55411c971",
"metadata": {},
"outputs": [],
"source": [
"#8\n",
"products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory=initialize_inventory(products)\n",
"customer_orders=get_customer_orders()\n",
"inventory=update_inventory(customer_orders, inventory)\n",
"order_statistics=calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory(inventory)\n",
"get_customer_orders_price(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b56143ef-d926-437e-9f2e-ae08b8cc6b33",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "d31a79c6-efcf-49b9-8966-103126be583a",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -90,7 +363,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.4"
}
},
"nbformat": 4,
Expand Down