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
109 changes: 107 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,116 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "28681018",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total products ordered: 1\n",
"Percentage of products ordered: 20.00%\n",
"Updated Inventory:\n",
"t-shirt: 10\n",
"mug: 19\n",
"hat: 30\n",
"book: 40\n",
"keychain: 50\n"
]
}
],
"source": [
"#previous data\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"#step 1\n",
"\n",
"def initialize_inventory(products):\n",
" for product in products:\n",
" inventory[product] = int(input(f\"Enter the quantity for {product}: \"))\n",
" return inventory\n",
"\n",
"#step 2\n",
"customer_orders = set()\n",
"def get_customer_orders():\n",
" while True:\n",
" p = input(\"Select a product to order:\")\n",
" if p in customer_orders:\n",
" print(\"You have already ordered this product.\")\n",
" else:\n",
" customer_orders.add(p)\n",
" b = input(\"Do you want to order more? (yes/no): \")\n",
" if b.lower() == \"no\":\n",
" break\n",
" return customer_orders\n",
"\n",
"#step 3\n",
"def update_inventory(customer_orders, inventory):\n",
" for order in customer_orders:\n",
" if order in inventory:\n",
" inventory[order] -= 1\n",
" return inventory\n",
"\n",
"#step 4\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = round((total_products_ordered / len(products)) * 100, 1)\n",
" order_statistics = (total_products_ordered, percentage_ordered)\n",
" return order_statistics\n",
"\n",
"#step 5\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" print(f\"Total products ordered: {order_statistics[0]}\")\n",
" print(f\"Percentage of products ordered: {order_statistics[1]:.2f}%\")\n",
" return order_statistics\n",
"\n",
"#step 6\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" for product in inventory:\n",
" print(f\"{product}: {inventory[product]}\")\n",
"\n",
"# Main program\n",
"inventory = {}\n",
"initialize_inventory(products)#to set up quantities\n",
"get_customer_orders()#to record the order from the customer\n",
"update_inventory(customer_orders, inventory)#to update the inventory after the order\n",
"order_statistics = calculate_order_statistics(customer_orders, products)#calculate statistics\n",
"print_order_statistics(order_statistics)#to print statistics\n",
"print_updated_inventory(inventory)#to print the updated inventory"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "3708fc0f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, 20.0)"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"calculate_order_statistics(customer_orders, products)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +166,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down