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

lab-error-handling-extra #26

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
211 changes: 211 additions & 0 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,217 @@
"source": [
"# your code goes here"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6e071ab2",
"metadata": {},
"outputs": [],
"source": [
"inventory = {}\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" validacion = 0\n",
" while validacion == 0:\n",
" try:\n",
" cantidad = int(input(f\"introduce la cantidad del producto que hay en el inventario: \"))\n",
" if cantidad < 0:\n",
" raise ValueError(\"Cantidad no valida. Por favor introduzca un numero positivo en digitos\")\n",
" validacion += 1\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
" inventory [product] = cantidad\n",
" return inventory\n",
"\n",
"\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = initialize_inventory(products)\n",
"print(f\"Elinventario de la tienda es: {inventory}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "57c9fc6b",
"metadata": {},
"outputs": [],
"source": [
"customer_orders = set()\n",
"finalpedido=0\n",
"\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" finalpedido = 0\n",
" while finalpedido != 1:\n",
" try:\n",
" pedido = str(input(f\"introduce el tipo de producto a comprar de la siguiente lista {products}: \"))\n",
" if pedido != str:\n",
" raise ValueError(f\"Producto no valida. Por favor introduzca un producto de la lista {products}.\")\n",
" finalpedido += 1\n",
" customer_orders.add(pedido)\n",
" otroproducto = input (\"Desea pedir mas productos (SI/NO): \")\n",
" otroproducto.lower()\n",
" if otroproducto == \"no\":\n",
" finalpedido += 1\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
" return customer_orders\n",
"\n",
"customer_orders = get_customer_orders()\n",
"print(f\"El pedido del cliente es: {customer_orders}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b4f63ebe",
"metadata": {},
"outputs": [],
"source": [
"\n",
"def calculate_total_price(customer_orders):\n",
" precio_total = ()\n",
" for producto in customer_orders:\n",
" try:\n",
" precio = float(input(f\"introduce el precio del producto {producto}: \")\n",
" if precio != float:\n",
" raise ValueError (\"Cantidad no valida. Por favor introduzca un dato valido\")\n",
" precio_total += precio\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
" return customer_orders\n",
"\n",
"customer_orders = get_customer_orders()\n",
"print(f\"El pedido del cliente es: {customer_orders}\")\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "546c51e2",
"metadata": {},
"outputs": [],
"source": [
"\n",
"def updated_inventory(customer_orders, inventory): \n",
" try:\n",
" return {product: inventory[product] - 1 if product in customer_orders else inventory[product] for product in inventory.keys() }\n",
" except TypeError: \n",
" return \"Error: Los parámetros deben ser listas.\" \n",
" except Exception as e: \n",
" return f\"Error inesperado: {e}\"\n",
"\n",
"inventory = updated_inventory(customer_orders, inventory)\n",
"print (f\"El inventario actual actualizado es : {inventory}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "95a115b6",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products): \n",
" try: \n",
" total_products_ordered = len(customer_orders) \n",
" percentage_ordered = (total_products_ordered * 100) / len(products) \n",
" return total_products_ordered, percentage_ordered \n",
" except ZeroDivisionError: \n",
" return \"Error: La lista de productos está vacía.\" \n",
" except TypeError: \n",
" return \"Error: Los parámetros deben ser listas.\" \n",
" except Exception as e: \n",
" return f\"Error inesperado: {e}\"\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"print (order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cb2ee3b9",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = (total_products_ordered*100)/len(products)\n",
" return total_products_ordered, percentage_ordered\n",
" \n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"print (order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bb94b075",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_stadistics):\n",
" try:\n",
" print(\"order statistics\")\n",
" print(f\"Total Products Ordered: {order_statistics[0]}\")\n",
" print(f\"Percentage of Products Ordered: {order_statistics[1]}%\")\n",
" except TypeError: \n",
" return \"Error: El tipo no es valido. Introduzca un tipo valido.\" \n",
" except Exception as e: \n",
" return f\"Error inesperado: {e}\"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "49af66dd",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" print(f\"El inventario actualizado es: {inventory}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "341b4d03",
"metadata": {},
"outputs": [],
"source": [
"def ejercutar_programa():\n",
" try:\n",
" initialize_inventory(products)\n",
" get_customer_orders()\n",
" updated_inventory(customer_orders, inventory)\n",
" calculate_order_statistics(customer_orders, products)\n",
" print_order_statistics(order_statistics)\n",
" print_updated_inventory(inventory)\n",
" except TypeError:\n",
" return \"Error: El tipo no es valido. Introduzca un tipo valido.\"\n",
" except Exception as e:\n",
" return f\"Error inesperado: {e}\"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b590d138",
"metadata": {},
"outputs": [],
"source": [
"ejercutar_programa()"
]
}
],
"metadata": {
Expand Down