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
137 changes: 135 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,144 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "2d41cc06",
"metadata": {},
"outputs": [],
"source": [
"products =[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "d558489c",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" cantidad = int(input(f\"Introduce la cantidad de {product}: \"))\n",
" inventory[product]= cantidad\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "acd9eed7",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" customer_orders = set()\n",
" add_another = \"yes\"\n",
" \n",
" while add_another == \"yes\" :\n",
" product = input(\"Qué producto quieres añadir?: \")\n",
" customer_orders.add(product)\n",
" add_another = input(\"Desea seguir añadiendo?\").lower()\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "c9dbb9e1",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "4c0b4fde",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_unique_products = (total_products_ordered / len(products)) *100\n",
"\n",
" return total_products_ordered, percentage_unique_products"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "7d518e7d",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" total, percentage = order_statistics\n",
" \n",
" print (\"order Statistics\")\n",
" print (\"------\")\n",
" print (f\"Total products ordered: {total}\")\n",
" print (f\"Percentage of unique products ordered: {percentage}%\")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "31d06684",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" print (\"updated inventory: \")\n",
" for product in inventory:\n",
" print(f\"{product}: {inventory[product]}\")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "d12e676c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"order Statistics\n",
"------\n",
"Total products ordered: 1\n",
"Percentage of unique products ordered: 20.0%\n",
"updated inventory: \n",
"t-shirt: 10\n",
"mug: 9\n",
"hat: 10\n",
"book: 10\n",
"keychain: 10\n"
]
}
],
"source": [
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"update_inventory(customer_orders, inventory)\n",
"order_statistics = calculate_order_statistics (customer_orders, products)\n",
"print_order_statistics (order_statistics)\n",
"print_updated_inventory (inventory)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +194,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down