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
171 changes: 170 additions & 1 deletion lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,175 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 97,
"id": "6fceac63-cc45-49b4-ba19-28998c50cf5f",
"metadata": {},
"outputs": [],
"source": [
"#1 Define a function named initialize_inventory that takes products as a parameter. \n",
"# Inside the function, implement the code for initializing the inventory dictionary using a loop and user input.\n",
"inventory = {}\n",
"products_list = []\n",
" \n",
"def initialize_inventory(products):\n",
" for product in products:\n",
" quantity = input(f\"add the quantity for {product}\")\n",
" if quantity.isdigit():\n",
" inventory[product] = int(quantity)\n",
" else:\n",
" inventory[product] = 10 #Default value\n",
"\n",
"#initialize_inventory(products_list)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 98,
"id": "9a74fefc-6013-4b1c-b760-6b3bdb12a012",
"metadata": {},
"outputs": [],
"source": [
"#2 Define a function named get_customer_orders that takes no parameters. \n",
"#Inside the function, implement the code for prompting the user to enter the product names using a loop. \n",
"#The function should return the customer_orders set\n",
"\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" for product in products_list:\n",
" want_to_buy = input(f\"Do you need a {product} : yes/no\").strip().lower()\n",
" if want_to_buy == \"yes\":\n",
" customer_orders.add(product)\n",
" return customer_orders\n",
"\n",
"#orders = get_customer_orders() "
]
},
{
"cell_type": "code",
"execution_count": 99,
"id": "8e25a6c7-39d5-43db-a736-f5f888f218b1",
"metadata": {},
"outputs": [],
"source": [
"#3 Define a function named update_inventory that takes customer_orders and inventory as parameters. \n",
"# Inside the function, implement the code for updating the inventory dictionary based on the customer orders\n",
"def update_inventory (customer_orders, inventory):\n",
" for order in customer_orders:\n",
" inventory[order] -= 1 \n",
"#update_inventory(orders, inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": 92,
"id": "29606623-d848-49d3-821c-9ad2a415f250",
"metadata": {},
"outputs": [],
"source": [
"# 4 Define a function named calculate_order_statistics that takes customer_orders and products as parameters. \n",
"#Inside the function, implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). \n",
"#The function should return these values.\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" stat_order = lambda x: float(\"{:.2f}\".format((x / len(products))*100))\n",
" order_statistics = { product: stat_order(1) if product in customer_orders else 0 for product in products }\n",
" return order_statistics \n",
" \n",
"#order_statistics = calculate_order_statistics(orders , products_list) \n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 93,
"id": "9642df4e-df71-41e8-9e49-de943cf075a9",
"metadata": {},
"outputs": [],
"source": [
"#5 Define a function named print_order_statistics that takes order_statistics as a parameter.\n",
"#Inside the function, implement the code for printing the order statistics.\n",
"def print_order_statistics(order_statistics):\n",
" for item_key , item_value in order_statistics.items():\n",
" print(f\"\"\" The {item_key} : is {item_value} % \"\"\")\n",
" \n",
"#print_order_statistics(order_statistics) "
]
},
{
"cell_type": "code",
"execution_count": 94,
"id": "96d91255-5b39-4d19-a9c0-2ecb09961a86",
"metadata": {},
"outputs": [],
"source": [
"#6 Define a function named print_updated_inventory that takes inventory as a parameter. \n",
"#Inside the function, implement the code for printing the updated inventory.\n",
"def print_updated_inventory(inventory):\n",
" for item, value in inventory.items():\n",
" print(f\" The remaing quantities for {item} : is {value} % \")\n",
"#print_updated_inventory(inventory) \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 95,
"id": "0a2795b6-3196-4cdb-9cf1-b568958c0133",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"add the quantity for hat 1à\n",
"add the quantity for book 20\n",
"add the quantity for mug 20\n",
"Do you need a hat : yes/no yes\n",
"Do you need a book : yes/no yes\n",
"Do you need a mug : yes/no no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" The hat : is 33.33 % \n",
" The book : is 33.33 % \n",
" The mug : is 0 % \n",
" The remaing quantities for hat : is 9 % \n",
" The remaing quantities for book : is 19 % \n",
" The remaing quantities for mug : is 20 % \n"
]
}
],
"source": [
"#7 Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"user_products = input(\"add a list of products separated by ,\")\n",
"try:\n",
" products_list = user_products.strip().split(\",\")\n",
" \n",
"except:\n",
" input(\"wrong input\")\n",
"initialize_inventory(products_list)\n",
"orders = get_customer_orders() \n",
"update_inventory(orders, inventory)\n",
"order_statistics = calculate_order_statistics(orders , products_list) \n",
"print_order_statistics(order_statistics) \n",
"print_updated_inventory(inventory) "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "90fe77aa-cbe5-4be5-8338-af2e88725162",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -61,7 +230,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down