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
94 changes: 92 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,101 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b949e30f",
"metadata": {},
"outputs": [],
"source": [
"def manage_orders():\n",
" def initialize_inventory(products):\n",
" inventory = {}\n",
"\n",
" for product in products:\n",
" quantity = input(f\"Enter quantity of {product}: \").strip()\n",
" \n",
" while not quantity.isdigit():\n",
" print(\"Please enter a number\")\n",
" quantity = input(f\"Enter quantity of {product}: \").strip()\n",
"\n",
" quantity = int(quantity)\n",
" inventory[product] = quantity\n",
" \n",
" return inventory\n",
"\n",
" def get_customer_orders():\n",
" customer_orders = set()\n",
"\n",
" while True:\n",
" order = input('Enter the name of the product to purchase: ').strip().lower()\n",
" \n",
" while order not in products:\n",
" print (f\"'{order}' is not in our inventory. Please write it correctly.\")\n",
" order = input('Enter the name of the product to purchase: ').strip().lower()\n",
" \n",
" customer_orders.add(order)\n",
"\n",
" validation = input(\"Do you want to buy more items? (yes/no): \").strip().lower()\n",
"\n",
" while validation not in (\"no\", \"yes\"):\n",
" print(f\"'{validation}' is not a correct option. Please select 'yes' or 'no'.\")\n",
" validation = input(\"Do you want to buy more items? (yes/no): \").strip().lower()\n",
"\n",
" if validation == \"no\":\n",
" break\n",
"\n",
" print (f\"The customer has ordered {customer_orders}\" \"\\n\")\n",
" return customer_orders\n",
"\n",
" def update_inventory(customer_orders,inventory):\n",
" for item in customer_orders:\n",
" inventory[item] -= 1\n",
" return inventory\n",
"\n",
" def calculate_order_statistics(customer_orders,products):\n",
"\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = round((total_products_ordered/len(products))*100,1)\n",
"\n",
" return total_products_ordered, percentage_ordered\n",
"\n",
" def print_order_statistics(order_statistics):\n",
"\n",
" total_products_ordered, percentage_of_products_ordered = order_statistics\n",
" print(\"------Order Statistics------\")\n",
" print(f\"Total products ordered: {total_products_ordered}\")\n",
" print(f\"Percentage of products ordered: {percentage_of_products_ordered}%\")\n",
"\n",
" def print_updated_inventory(inventory):\n",
" print(\"------Updated inventory------\")\n",
" for key,value in inventory.items():\n",
" print(f\"The current quantity of item '{key}' is: {value}\")\n",
"\n",
" products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
" inventory = initialize_inventory(products) \n",
" print (\"Available products\", products)\n",
" print()\n",
" customer_orders = get_customer_orders()\n",
" inventory = update_inventory(customer_orders, inventory)\n",
" order_statistics = calculate_order_statistics(customer_orders,products)\n",
" print_order_statistics(order_statistics)\n",
" print()\n",
" print_updated_inventory(inventory)"
]
},
{
"cell_type": "markdown",
"id": "703a979d",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +151,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.4"
}
},
"nbformat": 4,
Expand Down