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
213 changes: 212 additions & 1 deletion lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,217 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "60a6c958-9113-4b22-b16f-ea300626bc0a",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"enter a quantity for t-shirt: 2\n",
"enter a quantity for mug: 3\n",
"enter a quantity for hat: 1\n",
"enter a quantity for book: 5\n",
"enter a quantity for keychain: 7\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initialized inventory: {'t-shirt': 2, 'mug': 3, 'hat': 1, 'book': 5, 'keychain': 7}\n"
]
}
],
"source": [
"1.\n",
"products_list =[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory (products_list):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(f\"enter a quantity for {product}:\"))\n",
" inventory[product] = quantity\n",
" \n",
" return inventory\n",
"\n",
"inventory = initialize_inventory(products_list)\n",
"print(\"Initialized inventory:\", inventory)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "95940a1f-c61c-4bc7-8a9c-373e3779300d",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of a product to order: t-shirt\n",
"Do you want to add another product: Yes ou No? no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer orders are: {'t-shirt'}\n"
]
}
],
"source": [
"2.\n",
"def get_customer_orders ():\n",
" customer_orders = set ()\n",
" while True:\n",
" product = (input(\"Enter the name of a product to order:\"))\n",
" customer_orders.add(product)\n",
" answer = input(\"Do you want to add another product: Yes ou No?\")\n",
" if answer.lower() != \"Yes\":\n",
" break\n",
" print(\"Customer orders are:\", customer_orders) \n",
"get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "63d06a11-e2b2-4ea9-ba17-0f020a72b22a",
"metadata": {},
"outputs": [],
"source": [
"3.\n",
"\n",
"def update_inventory (customer_orders , inventory):\n",
" for product in costumer_order:\n",
" if product in inventory:\n",
" inventory [product] -= 1\n",
" return inventory\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "d704c4f8-4283-43d1-8887-e71a57e8fd59",
"metadata": {},
"outputs": [],
"source": [
"4.\n",
"\n",
"import math\n",
"def calculate_order_statistics (ustomer_orders , products):\n",
" total_ordered = len(customer_orders)\n",
" total_product = len (products)\n",
" percentage_unique = (total_ordered/total_product)*100\n",
" return total_ordered, percentage unique"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "26fbe696-7ed0-4304-8038-190ffce07bda",
"metadata": {},
"outputs": [],
"source": [
"5.\n",
"\n",
"def print_order_statistics ( order_statistics ):\n",
" total_ordered, percentage_unique = order_statistique\n",
" print (f\"total products ordered:{total_ordered}\")\n",
" print (f\"percentage of unique products ordered: {percentage_unique}\")"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "f86ce94d-e6f6-4243-90cd-0c4d7861e5da",
"metadata": {},
"outputs": [],
"source": [
"6.\n",
"\n",
"def print_updated_inventory (inventory):\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "c391fb3d-e71d-426d-a695-019504a44bd0",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"enter a quantity for t-shirt: 4\n",
"enter a quantity for mug: 3\n",
"enter a quantity for hat: 8\n",
"Enter the name of a product to order: t-shirt\n",
"Do you want to add another product: Yes ou No? yes\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer orders are: {'t-shirt'}\n"
]
},
{
"ename": "TypeError",
"evalue": "object of type 'NoneType' has no len()",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mTypeError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[27]\u001b[39m\u001b[32m, line 8\u001b[39m\n\u001b[32m 4\u001b[39m inventory = initialize_inventory(products)\n\u001b[32m 6\u001b[39m customer_orders = get_customer_orders()\n\u001b[32m----> \u001b[39m\u001b[32m8\u001b[39m order_statistics = \u001b[43mcalculate_order_statistics\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcustomer_orders\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mproducts\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 10\u001b[39m updated_inventory = update_inventory(customer_orders, inventory)\n\u001b[32m 12\u001b[39m print_order_statistics(order_statistics)\n",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[20]\u001b[39m\u001b[32m, line 5\u001b[39m, in \u001b[36mcalculate_order_statistics\u001b[39m\u001b[34m(ustomer_orders, products)\u001b[39m\n\u001b[32m 4\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mcalculate_order_statistics\u001b[39m (ustomer_orders , products):\n\u001b[32m----> \u001b[39m\u001b[32m5\u001b[39m total_ordered = \u001b[38;5;28;43mlen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mcustomer_orders\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 6\u001b[39m total_product = \u001b[38;5;28mlen\u001b[39m (products)\n\u001b[32m 7\u001b[39m percentage = (total_ordered/total_product)*\u001b[32m100\u001b[39m\n",
"\u001b[31mTypeError\u001b[39m: object of type 'NoneType' has no len()"
]
}
],
"source": [
"7. \n",
"products = [\"t-shirt\", \"mug\", \"hat\"]\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"customer_orders = get_customer_orders()\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"\n",
"updated_inventory = update_inventory(customer_orders, inventory)\n",
"\n",
"print_order_statistics(order_statistics)\n",
"\n",
"print_updated_inventory(updated_inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ce314f8a-bf37-4773-bd01-9d7bde50705a",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "bb99d277-2dd6-4fe6-9ad9-eb7533a1459b",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -61,7 +272,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.11"
}
},
"nbformat": 4,
Expand Down