Skip to content

Solve lab #23

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: master
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
258 changes: 255 additions & 3 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,264 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 59,
"id": "cc2c441d-9dcf-4817-b097-cf6cbe440846",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"products = ['apple', 'banana']\n",
"def initialize_inventory(inventory):\n",
" inventory = {}\n",
" for product in products:\n",
" valid = False\n",
" while not valid:\n",
" try:\n",
" quantity = int(input(f\"Enter quantity for {product}: \"))\n",
" if quantity >= 0:\n",
" inventory[product] = quantity\n",
" valid = True\n",
" else:\n",
" print(\"Quantity can't be negative.\")\n",
" except ValueError:\n",
" print(\"Please enter a valid number.\")\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 63,
"id": "186b98ef-c666-402c-9daa-3241055e3841",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders(inventory):\n",
" valid_input = False\n",
" while not valid_input:\n",
" try:\n",
" num_orders = int(input('Enter the number of customer orders: '))\n",
" if num_orders >= 0:\n",
" valid_input = True\n",
" else:\n",
" print(\"Number of orders cannot be negative.\")\n",
" except ValueError:\n",
" print(\"Enter a valid number.\")\n",
"\n",
" orders = set()\n",
" for _ in range(num_orders):\n",
" valid_product = False\n",
" while not valid_product:\n",
" product = input('Enter product name: ')\n",
" if product in inventory and inventory[product] > 0:\n",
" orders.add(product)\n",
" valid_product = True\n",
" else:\n",
" print(f\"{product} is unavailable.\")\n",
" return orders"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "a3446ca2-31e8-45ff-911b-f950574ae7ed",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(orders, inventory):\n",
" for product in orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1\n",
" if inventory[product] == 0:\n",
" del inventory[product]\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "7773bab6-8c5d-4370-90c0-898f6a779762",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(orders, all_products):\n",
" total_count = len(orders)\n",
" percentage = (total_count / len(all_products)) * 100\n",
" return total_count, percentage"
]
},
{
"cell_type": "code",
"execution_count": 69,
"id": "ae99fd24-1c07-49f9-8db8-8142fc2580b4",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(stats):\n",
" print(\"Order Statistics:\")\n",
" print(f\"Total Products Ordered: {stats[0]}\")\n",
" print(f\"Percentage of Products Ordered: {stats[1]:.2f}%\")"
]
},
{
"cell_type": "code",
"execution_count": 71,
"id": "9be4596f-2832-4131-a219-4ec86659aa76",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" for item, quantity in inventory.items():\n",
" print(f\"{item}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "ea47c83d-10bc-4496-b836-68ff9f65a18d",
"metadata": {},
"outputs": [],
"source": [
"def calculate_total_price(customer_orders):\n",
" total_price = 0\n",
" for product in customer_orders:\n",
" valid_input = False\n",
" while not valid_input:\n",
" try:\n",
" price = float(input(f\"Enter price for {product}: \"))\n",
" if price >= 0:\n",
" total_price += price\n",
" valid_input = True\n",
" else:\n",
" print(\"Price must be non-negative.\")\n",
" except ValueError:\n",
" print(\"Enter a valid number.\")\n",
" return total_price"
]
},
{
"cell_type": "code",
"execution_count": 75,
"id": "200f72b3-e98f-439d-9c8e-a8949d718b1a",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter quantity for apple: 3\n",
"Enter quantity for banana: 2\n"
]
}
],
"source": [
"inventory = initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 77,
"id": "5e6e301a-9324-4588-999b-b43d7e36390e",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the number of customer orders: 1\n",
"Enter product name: apple\n"
]
}
],
"source": [
"customer_orders = get_customer_orders(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 81,
"id": "e7a19861-4407-46a8-aa1d-a1dfc53b7f04",
"metadata": {},
"outputs": [],
"source": [
"updated_inventory = update_inventory(customer_orders, inventory)"
]
},
{
"cell_type": "code",
"execution_count": 85,
"id": "52607b72-34eb-4ec2-a62c-1f237da8940b",
"metadata": {},
"outputs": [],
"source": [
"order_stats = calculate_order_statistics(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": 89,
"id": "d6162d0d-aaa8-4af0-a309-9c889e8b13d4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 1\n",
"Percentage of Products Ordered: 50.00%\n"
]
}
],
"source": [
"print_order_statistics(order_stats)"
]
},
{
"cell_type": "code",
"execution_count": 91,
"id": "57a7103e-c657-4b76-a0e8-7e1aa2c311e0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated Inventory:\n",
"apple: 2\n",
"banana: 2\n"
]
}
],
"source": [
"print_updated_inventory(updated_inventory)"
]
},
{
"cell_type": "code",
"execution_count": 94,
"id": "d8b1694a-c29e-4d96-9045-031e1155068f",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter price for apple: 10\n"
]
},
{
"data": {
"text/plain": [
"10.0"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"calculate_total_price(customer_orders)"
]
}
],
Expand All @@ -66,7 +318,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.4"
}
},
"nbformat": 4,
Expand Down