Skip to content
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

Valeria Solved Lab #236

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
208 changes: 207 additions & 1 deletion lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,212 @@
"\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": 25,
"id": "443eddf4",
"metadata": {},
"outputs": [],
"source": [
"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\"Indicate the quantity of {product}s available: \"))\n",
" if quantity >= 0:\n",
" inventory[product] = quantity\n",
" valid_input = True\n",
" else:\n",
" print(f\"Invalid quantity of {product}s! Please enter a non-negative value.\")\n",
" except ValueError:\n",
" print(\"Invalid input! Please use only numbers to indicate the quantity of products.\")\n",
" print(\"INITIAL INVENTORY\\n\")\n",
" for product, amount in inventory.items():\n",
" print(f\"Product: {product} - Amount: {amount}\")\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "281692b9",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders(inventory):\n",
" customer_orders = set()\n",
" valid_input = False\n",
" while valid_input == False:\n",
" try:\n",
" amount_ordered = int(input(\"Enter the number of customer orders:\"))\n",
" if amount_ordered > 0:\n",
" valid_input = True\n",
" else:\n",
" print(\"Please, enter a whole, positive number.\")\n",
" except ValueError:\n",
" print(\"Please, use only whole numbers to enter the number of customer orders.\")\n",
" \n",
" for i in range(1,amount_ordered+1):\n",
" valid_input = False\n",
" while valid_input == False:\n",
" try:\n",
" order = input(\"Input a product the costumer want to order:\").lower().strip()\n",
" if order in inventory.keys() and inventory[order]>0:\n",
" customer_orders.add(order)\n",
" valid_input=True\n",
" else:\n",
" print(f\"The product you're trying to order ({order}) isn't available or is out of stock. Please try with another product from: {list(inventory.keys())})\")\n",
" except Exception as e:\n",
" print(f\"An error has ocurred. Error: {e}\")\n",
"\n",
" print(\"\\nOrder complete!\\n\")\n",
" for product in customer_orders:\n",
" print(f\"Product ordered: {product}\") \n",
" return(customer_orders)\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b14f124d",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" updated_inventory = {product:inventory[product]-1 if product in customer_orders else inventory[product] for product in inventory}\n",
" cleaned_inventory = {product: quantity for product, quantity in updated_inventory.items() if quantity > 0}\n",
" return cleaned_inventory"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "517a0182",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" order_status = []\n",
" amount_ordered = len(customer_orders)\n",
" amount_inventory = sum(inventory.values())\n",
"\n",
" if amount_inventory > 0:\n",
" percentage_ordered = (amount_ordered * 100) / amount_inventory\n",
" else:\n",
" percentage_ordered = 0\n",
"\n",
" order_status.append(amount_ordered)\n",
" order_status.append(percentage_ordered)\n",
" \n",
" return order_status"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "936b4d6c",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_status):\n",
" amount_ordered = order_status[0]\n",
" percentage_ordered = order_status[1]\n",
" print(f\"\\nOrder Statistics:\\nTotal Products Ordered: {amount_ordered}\\nPercentage of Products Ordered: {percentage_ordered}%\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "f62f156b",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" print(\"\\nUPDATED INVENTORY\\n\")\n",
" [print(f\"Product: {product} - Amount: {amount}\") for product, amount in inventory.items()]"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "8ccb5604",
"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\"Please, enter the price of {product}\"))\n",
" if price >= 0:\n",
" total_price += price\n",
" valid_input = True\n",
" else:\n",
" print(f\"Invalid price of {product}. Please, enter a non-negative value.\")\n",
" except ValueError:\n",
" print(\"Invalid input! Please, use only numbers and dots to indicate the price of the product.\")\n",
" print(f\"\\nTotal Price: {total_price}€\")\n",
" return total_price"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "eb92fe4e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"INITIAL INVENTORY\n",
"\n",
"Product: t-shirt - Amount: 5\n",
"Product: mug - Amount: 4\n",
"Product: hat - Amount: 0\n",
"Product: book - Amount: 2\n",
"Product: keychain - Amount: 4\n",
"The product you're trying to order (hat) isn't available or is out of stock. Please try with another product from: ['t-shirt', 'mug', 'hat', 'book', 'keychain'])\n",
"\n",
"Order complete!\n",
"\n",
"Product ordered: book\n",
"Product ordered: keychain\n",
"\n",
"Total Price: 12.5€\n",
"\n",
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 13.333333333333334%\n",
"\n",
"UPDATED INVENTORY\n",
"\n",
"Product: t-shirt - Amount: 5\n",
"Product: mug - Amount: 4\n",
"Product: book - Amount: 1\n",
"Product: keychain - Amount: 3\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders(inventory)\n",
"order_status = calculate_order_statistics(customer_orders, products)\n",
"total_price = calculate_total_price(customer_orders)\n",
"inventory = update_inventory(customer_orders,inventory)\n",
"print_order_statistics(order_status)\n",
"print_updated_inventory(inventory)"
]
}
],
"metadata": {
Expand All @@ -90,7 +296,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.6"
}
},
"nbformat": 4,
Expand Down