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
173 changes: 171 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,180 @@
"\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": "2ce25673",
"metadata": {},
"outputs": [],
"source": [
"def manage_orders():\n",
" def initialize_inventory(products):\n",
" inventory = {}\n",
" products = [item.lower().strip() for item in products]\n",
" for product in products:\n",
" while True:\n",
" try:\n",
" quantity = int(input(f\"Enter quantity of {product}: \").strip())\n",
" if quantity >= 0:\n",
" inventory[product] = quantity\n",
" break\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",
" \n",
" return inventory\n",
"\n",
" def get_customer_orders(inventory):\n",
" customer_orders = set()\n",
"\n",
" while True:\n",
" try:\n",
" order = input('Enter the name of the product to purchase: ').strip().lower()\n",
"\n",
" if order not in inventory:\n",
" raise KeyError\n",
" if inventory[order] <= 0:\n",
" raise ValueError\n",
" \n",
" customer_orders.add(order)\n",
" inventory[order] -= 1\n",
" except KeyError:\n",
" print(f'Product {order} not found')\n",
" except ValueError:\n",
" print(f'Sorry, {order} is out of stock')\n",
" \n",
" validation = input(\"Do you want to buy more items? (yes/no): \").strip().lower()\n",
" while validation not in ('yes','no'):\n",
" print(f\"'{validation}' is not a correct option. Please select 'yes' or 'no'.\", \"\\n\")\n",
" validation = input(\"Do you want to buy more items? (yes/no): \").strip().lower() \n",
"\n",
" if validation == \"no\":\n",
" break \n",
"\n",
" print(\"------Customer´s order------\")\n",
" print (f\"The customer has ordered {customer_orders}\", \"\\n\")\n",
"\n",
" return customer_orders\n",
" \n",
" def calculate_order_statistics(customer_orders,products):\n",
"\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = round((total_products_ordered/len(products))*100,1)\n",
"\n",
" return total_products_ordered, percentage_ordered\n",
"\n",
" def print_order_statistics(order_statistics):\n",
"\n",
" total_products_ordered, percentage_of_products_ordered = order_statistics\n",
" print(\"------Order Statistics------\")\n",
" print(f\"Total products ordered: {total_products_ordered}\")\n",
" print(f\"Percentage of products ordered: {percentage_of_products_ordered}%\",\"\\n\")\n",
"\n",
" def print_updated_inventory(inventory):\n",
" print(\"------Updated inventory------\")\n",
" for key,value in inventory.items():\n",
" print(f\"{key}: {value}\")\n",
" print()\n",
"\n",
" def calculate_total_price(customer_orders):\n",
"\n",
" prices_list = []\n",
" for product in customer_orders:\n",
"\n",
" while True:\n",
" try:\n",
" user_input = float(input(f'Enter the price of one {product}').replace(',','.'))\n",
" if user_input >= 0:\n",
" prices_list.append(user_input)\n",
" break\n",
" else:\n",
" print('Price cannot be negative. Please enter a valid price')\n",
" except ValueError:\n",
" print('Invalid input. Price has be a number.')\n",
"\n",
" print('------Receipt------')\n",
" for price,item in zip(prices_list,list(customer_orders)):\n",
" print(f'The price of one {item} is: {price}$ ') \n",
"\n",
" sum_prices = round(sum(prices_list),1)\n",
" \n",
" return sum_prices\n",
"\n",
" products = [\"T-shirt\", \"MUg\", \"hat\", \"bOok\", \"keychain\"]\n",
"\n",
" inventory = initialize_inventory(products) \n",
" print(\"------Availability of products------\")\n",
" for item,value in inventory.items():\n",
" print(item,\":\",value) \n",
" customer_orders = get_customer_orders(inventory)\n",
" order_statistics = calculate_order_statistics(customer_orders,products)\n",
" print_order_statistics(order_statistics)\n",
" print_updated_inventory(inventory)\n",
" total = calculate_total_price(customer_orders)\n",
" print(f'the total price is: {total}$')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "d492c33d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------Availability of products------\n",
"t-shirt : 1\n",
"mug : 1\n",
"hat : 1\n",
"book : 1\n",
"keychain : 1\n",
"Product mu not found\n",
"Sorry, mug is out of stock\n",
"'45' is not a correct option. Please select 'yes' or 'no'. \n",
"\n",
"------Customer´s order------\n",
"The customer has ordered {'mug'} \n",
"\n",
"------Order Statistics------\n",
"Total products ordered: 1\n",
"Percentage of products ordered: 20.0% \n",
"\n",
"------Updated inventory------\n",
"t-shirt: 1\n",
"mug: 0\n",
"hat: 1\n",
"book: 1\n",
"keychain: 1\n",
"\n",
"Invalid input. Price must be a number.\n",
"Price cannot be negative. Please enter a valid price\n",
"------Receipt------\n",
"The price of one mug is: 34.5$ \n",
"the total price is: 34.5$\n"
]
}
],
"source": [
"manage_orders()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2fa4bb28",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +259,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.4"
}
},
"nbformat": 4,
Expand Down