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
295 changes: 291 additions & 4 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,305 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 54,
"id": "cc2c441d-9dcf-4817-b097-cf6cbe440846",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"# Products\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "63717daf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 5, 'hat': 0, 'book': 5, 'keychain': 5}\n"
]
}
],
"source": [
"def initialize_inventory(products):\n",
" \"\"\"\n",
" Initialize inventory by asking the user to enter the quantity of the procucts.\n",
" \"\"\"\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",
"inventory = initialize_inventory(products)\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "a502b74f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the number of customer orders: 2.\n",
"Error: No stock available for hat. Please, try again\n",
"Enter the name of the product the customer wants to order: book\n",
"Enter the name of the product the customer wants to order: mug\n"
]
}
],
"source": [
"def get_customer_orders(products, inventory):\n",
" \"\"\"\n",
" It prompts the user to enter the number of customer orders and gathers the product names using \n",
" a loop and user input.\n",
" \"\"\"\n",
" valid_orders = False\n",
" while not valid_orders:\n",
" try:\n",
" number_of_orders = int(input('Enter the number of customer orders: ').strip())\n",
" if number_of_orders < 0:\n",
" raise ValueError(\"The number of orders needs to be >= 0.\")\n",
" \n",
" valid_orders = True\n",
" except ValueError as e:\n",
" print(f\"Error: {e}\")\n",
"\n",
" print(f'Enter the number of customer orders: {number_of_orders}.')\n",
"\n",
" customer_orders = set()\n",
" \n",
" for n in range (0, number_of_orders):\n",
" while True:\n",
" try:\n",
" product = input(f'Enter the name of a product from {products}').strip()\n",
" if product not in inventory:\n",
" raise ValueError(f'Choose betweeen: {list(inventory.keys())}')\n",
" \n",
" if inventory[product] <= 0:\n",
" raise ValueError(f'No stock available for {product}')\n",
" customer_orders.add(product)\n",
" break\n",
" except ValueError as e:\n",
" print(f\"Error: {e}. Please, try again\")\n",
"\n",
" for x in customer_orders:\n",
" print(f'Enter the name of the product the customer wants to order: {x}')\n",
"\n",
" return customer_orders\n",
"\n",
"customer_orders = get_customer_orders(products, inventory)"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "d0501e53",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Program has succesfully finished work.\n"
]
}
],
"source": [
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" \"\"\"\n",
" It takes customer_orders and products and it calculates the order statistics.\n",
" \"\"\"\n",
" try:\n",
" if len(customer_orders) <= 0:\n",
" raise ValueError('Statistics cannot be processed because there are no customer orders.')\n",
" if products == []:\n",
" raise ValueError('Products is empty.')\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_of_products_ordered = total_products_ordered/len(products)*100\n",
" except ZeroDivisionError as e:\n",
" print(f'Error: {e}')\n",
" except ValueError as e:\n",
" print(f'Error: {e}')\n",
" except TypeError as e:\n",
" print(f'Error: {e}')\n",
" else:\n",
" return (total_products_ordered, percentage_of_products_ordered)\n",
" \n",
" finally:\n",
" print('Program has succesfully finished work.')\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "ceb7a212",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: <2>.\n",
"Percentage of Products Ordered: <40.0>%\n",
"The program has ended correctly. Report any issue to the programmer\n"
]
}
],
"source": [
"def print_order_statistics(order_statistics):\n",
" \"\"\"\n",
" It takes `order_statistics` as a parameter and it prints the order statistics.\n",
" \"\"\"\n",
" try:\n",
" print(\n",
" f'Order Statistics:\\n'\n",
" f'Total Products Ordered: <{order_statistics[0]}>.\\n'\n",
" f'Percentage of Products Ordered: <{order_statistics[1]}>%'\n",
" )\n",
" except TypeError as e:\n",
" print(f'Error: {e}')\n",
" except IndexError as e:\n",
" print(f'Error: {e}')\n",
" finally:\n",
" print('The program has ended correctly. Report any issue to the programmer')\n",
" \n",
"print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "3d425ea6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Program has ended correctly. Report any issue.\n"
]
},
{
"data": {
"text/plain": [
"{'t-shirt': 5, 'mug': 4, 'book': 4, 'keychain': 5}"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"def update_inventory(customer_orders, inventory):\n",
" \"\"\"\n",
" It takes customer_orders and inventory and it updates the inventory dictionary based on the customer orders\n",
" and it removes the product from the inventory if its quantity becomes zero after fulfilling the customer orders \n",
" using comprehension to filter out the products with a quantity of zero from the inventory.\n",
" \"\"\"\n",
" try:\n",
" updated_inventory = {\n",
" product: (value - 1 if product in customer_orders else value)\n",
" for product, value in inventory.items()\n",
" }\n",
" updated_inventory = {product: value for product, value in updated_inventory.items() if value != 0}\n",
" except AttributeError as e:\n",
" print(f'Error: {e}')\n",
" else:\n",
" return updated_inventory\n",
" finally:\n",
" print('Program has ended correctly. Report any issue.')\n",
"\n",
"update_inventory(customer_orders, inventory)"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "c4c31405",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: It needs to be a positive number\n",
"Error: It needs to be a positive number\n",
"Error: It needs to be a positive number\n",
"Total price: 2.0€\n"
]
},
{
"data": {
"text/plain": [
"2.0"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def calculate_total_price(products, customer_orders):\n",
" \"\"\"\n",
" It calculate total price by asking customer to enter the price of the product.\n",
" \"\"\"\n",
" prices = {}\n",
"\n",
" valid_price = False\n",
"\n",
" for product in customer_orders:\n",
"\n",
" while True:\n",
"\n",
" try:\n",
" price = float(input(f'Enter the price of {product}: '))\n",
"\n",
" if price <= 0:\n",
" raise ValueError(\"It needs to be a positive number\")\n",
" \n",
" prices[product] = price\n",
" break\n",
" except ValueError as e:\n",
" print(f'Error: {e}')\n",
"\n",
" total_price = sum(prices.values())\n",
"\n",
" print(f'Total price: {total_price}€')\n",
"\n",
" return total_price\n",
"\n",
"calculate_total_price(products, customer_orders)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -66,7 +353,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down