Skip to content

Lab finished #433

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: 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
202 changes: 200 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,209 @@
"\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": "ba1f36a0",
"metadata": {},
"outputs": [],
"source": [
"# def get_customer_orders(inventory):\n",
"# customer_orders = 0\n",
"# order_products = []\n",
" \n",
" \n",
" \n",
"# while customer_orders == 0:\n",
"# num_orders= int(input(\"Please indicate if you're placing 1 or 2 customer orders: \").strip())\n",
"# try:\n",
"# if num_orders == 2:\n",
"# customer_orders= 2\n",
"# break\n",
"# elif num_orders == 1:\n",
"# continue_ordering = input(\"Are you sure you want to place just one order? (yes/no): \").lower()\n",
"# while continue_ordering not in [\"yes\", \"no\"]: # entrada valida\n",
"# continue_ordering = input(\"Please enter a valid answer. Do you want to continue ordering? (yes/no): \").lower()\n",
"# if continue_ordering == \"no\":\n",
"# customer_orders = 2\n",
"# elif continue_ordering == \"yes\":\n",
"# customer_orders= 1\n",
"# except:\n",
"# print(\"Please enter a number between 1 and 2\") \n",
"# # print(customer_orders)\n",
"# # print(type(customer_orders))\n",
"# if customer_orders > 0:\n",
"# try:\n",
"# while int(len(order_products)) < int(customer_orders):\n",
"# for i in range(int(customer_orders)): \n",
"# product_name = input(f\"Enter the name of product #{i+1} that goes in the order: \").strip().lower()\n",
"# if product_name in inventory.keys(): \n",
"# order_products.append(product_name) \n",
"# break \n",
"# else:\n",
"# print(f\"'{product_name}' is not in the inventory. Please enter a valid product\")\n",
"# except KeyboardInterrupt:\n",
"# print(\"\\nOrder process interrupted by user during product input. Order creation stopped.\")\n",
"# return set(order_products)\n",
"\n",
"# def calculate_total_prize(order_products):\n",
"# # prices={prood:float(input(f\"Enter the value of {prood}: \"))for prood in order_products}\n",
"# prices=0\n",
"# while prices <= 0:\n",
"# try:\n",
"# prices= sum([float(input(f\"Enter the value of {value}: \")) for value in order_products])\n",
"# if prices > 0:\n",
"# break \n",
"# else:\n",
"# print(\"Price must be greater than 0\") \n",
"# except ValueError:\n",
"# print(\"Please enter a valid price for the product(s).\")\n",
"# return prices\n",
"\n",
"\n",
"# inventory = {\"t-shirt\":1, \"mug\":1, \"hat\":1, \"book\":1, \"keychain\":1}\n",
"# order_products=get_customer_orders(inventory)\n",
"# calculate_total_prize(order_products)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ebd7f7e5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'vas' is not in the inventory. Please enter a valid product\n",
"'hats' is not in the inventory. Please enter a valid product\n",
"{'book', 'hat'} 44\n",
"Order Statistics:\n",
"Total products ordered: 2\n",
"Percentage of products ordered: 40.00%\n",
"Updated inventory:\n",
"t-shirt: 1\n",
"mug: 1\n",
"keychain: 1\n",
"Total Price 44.0\n"
]
}
],
"source": [
"def get_customer_orders(inventory):\n",
" customer_orders = 0\n",
" order_products = []\n",
" \n",
" while customer_orders == 0:\n",
" num_orders= int(input(\"Please indicate if you're placing 1 or 2 customer orders: \").strip())\n",
" try:\n",
" if num_orders == 2:\n",
" customer_orders= 2\n",
" break\n",
" elif num_orders == 1:\n",
" continue_ordering = input(\"Are you sure you want to place just one order? (yes/no): \").lower()\n",
" while continue_ordering not in [\"yes\", \"no\"]: # entrada valida\n",
" continue_ordering = input(\"Please enter a valid answer. Do you want to continue ordering? (yes/no): \").lower()\n",
" if continue_ordering == \"no\":\n",
" customer_orders = 2\n",
" elif continue_ordering == \"yes\":\n",
" customer_orders= 1\n",
" except:\n",
" print(\"Please enter a number between 1 and 2\") \n",
" if customer_orders > 0:\n",
" try:\n",
" while int(len(order_products)) < int(customer_orders):\n",
" for i in range(int(customer_orders)): \n",
" product_name = input(f\"Enter the name of product #{i+1} that goes in the order: \").lower()\n",
" if product_name in inventory.keys(): \n",
" order_products.append(product_name) \n",
" else:\n",
" print(f\"'{product_name}' is not in the inventory. Please enter a valid product\")\n",
" except KeyboardInterrupt:\n",
" print(\"\\nOrder process interrupted by user during product input. Order creation stopped.\")\n",
" \n",
" return (customer_orders, int(num_orders), set(order_products))\n",
"\n",
"def initialize_inventory(products):\n",
" \n",
" \"\"\" This is desing to get input for the inventory\"\"\"\n",
" inventory = {product: int(input(f\"Enter the initial amount for product: {product} in the inventory\")) for product in products} \n",
"\n",
" return inventory \n",
" \n",
"def update_inventory(order_products, inventory):\n",
" \"\"\"This function updates the inventory based on customer orders\"\"\"\n",
" print(order_products,\"44\")\n",
" for item in order_products:\n",
" if item in inventory:\n",
" inventory[item] = inventory[item] -1 \n",
" \n",
" updated_inventory= {key: value for key, value in inventory.items() if value > 0}\n",
" return updated_inventory\n",
" \n",
"def calculate_order_statistics(num_orders, inventory):\n",
" \n",
" sum_inventory_final=len(inventory.keys())\n",
" total_products_percentage=(num_orders/sum_inventory_final*100) \n",
" order_statistics = (total_products_percentage, num_orders)\n",
" return order_statistics\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" \n",
" print(\"Order Statistics:\")\n",
" print(\"Total products ordered: \", order_statistics[1]) \n",
" print(f\"Percentage of products ordered: {order_statistics[0]:.2f}%\") \n",
"\n",
" return order_statistics\n",
"\n",
"def print_updated_inventory(updated_inventory):\n",
" print(\"Updated inventory:\")\n",
" for product,value in updated_inventory.items():\n",
" print(f\"{product}: {value}\")\n",
"\n",
"def calculate_total_prize(order_products):\n",
" prices=0\n",
" while prices <= 0:\n",
" try:\n",
" prices= sum([float(input(f\"Enter the value of {value}: \")) for value in order_products])\n",
" if prices > 0:\n",
" break \n",
" else:\n",
" print(\"Price must be greater than 0\") \n",
" except ValueError:\n",
" print(\"Please enter a valid price for the product(s).\")\n",
" return prices\n",
"\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = initialize_inventory(products)\n",
"customer_orders, num_orders, order_products= get_customer_orders(inventory)\n",
"updated_inventory= update_inventory(order_products, inventory)\n",
"prices=calculate_total_prize(order_products)\n",
"order_statistics= calculate_order_statistics(num_orders, inventory)\n",
"\n",
"\n",
"print_order_statistics(order_statistics)\n",
"\n",
"print_updated_inventory(updated_inventory)\n",
"print(f\"Total Price {prices}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6467d188",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +288,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.3"
}
},
"nbformat": 4,
Expand Down