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
197 changes: 195 additions & 2 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,204 @@
"\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f7e8c06e",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ab862057",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory = {product: int(input(\"Enter the available quantity of \" + product + \":\")) for product in products}\n",
" return inventory\n",
"print(inventory := initialize_inventory(products))"
]
},
{
"cell_type": "markdown",
"id": "f646f20a",
"metadata": {},
"source": [
"Modify the function get_customer_orders so it prompts the user to enter the number of customer orders and gathers the product names using a loop and user input."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c5fe6584",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" customer_orders = set()\n",
" orders_quantity = int(input(\"Enter the number of customer orders:\"))\n",
" if orders_quantity <= 0:\n",
" print(\"You must order at least one product.\")\n",
" else:\n",
" while orders_quantity > len(customer_orders):\n",
" order = input(\"Enter the name of the product a customer wants to order:\").strip().lower()\n",
" if order in inventory:\n",
" if inventory[order] <= 0:\n",
" print(f\"Sorry, {order} is out of stock.\")\n",
" continue\n",
" else:\n",
" customer_orders.add(order)\n",
" print(f\"Added {order} to your order.\")\n",
" else:\n",
" print(f\"Sorry, we don't have {order} in our inventory.\")\n",
" order_continue = input(\"Do you want to continue with the order? (yes/no)\").strip().lower()\n",
" if order_continue in ['no', 'yes']:\n",
" if order_continue == 'yes':\n",
" while orders_quantity > len(customer_orders):\n",
" order = input(\"Enter the name of the product a customer wants to order:\").strip().lower()\n",
" if order in inventory:\n",
" if inventory[order] <= 0:\n",
" print(f\"Sorry, {order} is out of stock.\")\n",
" continue\n",
" else:\n",
" customer_orders.add(order)\n",
" print(f\"Added {order} to your order.\")\n",
" else:\n",
" print(\"Order completed\")\n",
" break\n",
" else:\n",
" print(\"Invalid input. Please enter 'yes' or 'no'.\")\n",
" continue\n",
" return customer_orders\n",
"customer_orders = get_customer_orders()"
]
},
{
"cell_type": "markdown",
"id": "529e93ae",
"metadata": {},
"source": [
"*ORDER STATISTICS FUNCIONS*"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0e782160",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = (total_products_ordered / len(products)) * 100\n",
" return total_products_ordered, percentage_ordered\n",
"results = calculate_order_statistics(customer_orders, products)\n",
"total_products_ordered = results[0]\n",
"percentage_ordered = results[1]\n",
"\n",
"def print_order_statistics(total_products_ordered, percentage_ordered):\n",
" print(\"Order statistics:\")\n",
" print(\"Total products ordered:\", total_products_ordered)\n",
" print(\"Percentage of products ordered:\", percentage_ordered)\n",
"print_order_statistics(total_products_ordered, percentage_ordered)"
]
},
{
"cell_type": "markdown",
"id": "7b28f56a",
"metadata": {},
"source": [
"Add a new function to calculate the total price of the customer order. For each product in customer_orders, prompt the user to enter the price of that product. Use comprehension to calculate the total price. Note: assume that the user can only have 1 unit of each product."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "68692266",
"metadata": {},
"outputs": [],
"source": [
"def calculate_total_price(customer_orders):\n",
" calculate_total_price = sum([float(input(f\"Enter the price for {order}:\")) for order in customer_orders])\n",
" print(\"Total price of the order: €\", calculate_total_price)\n",
" return calculate_total_price"
]
},
{
"cell_type": "markdown",
"id": "95b957e7",
"metadata": {},
"source": [
"Modify the update_inventory function to remove the product from the inventory if its quantity becomes zero after fulfilling the customer orders. Use comprehension to filter out the products with a quantity of zero from the inventory."
]
},
{
"cell_type": "code",
"execution_count": 98,
"id": "7e8767a4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated inventory: {'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n"
]
}
],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" inventory = {product: inventory[product] - 1 if product in customer_orders and inventory[product] > 0 else inventory[product] for product in inventory}\n",
" inventory = {product: quantity for product, quantity in inventory.items() if quantity > 0}\n",
" print(\"Updated inventory:\", inventory)\n",
" return inventory\n",
"inventory = update_inventory(customer_orders, inventory)"
]
},
{
"cell_type": "markdown",
"id": "1af7c938",
"metadata": {},
"source": [
"FINAL FUNCTIONS LAUNCHING"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ff0c1260",
"metadata": {},
"outputs": [],
"source": [
"def launch_all():\n",
" inventory = initialize_inventory(products)\n",
" customer_orders = get_customer_orders()\n",
" calculate_order_statistics(customer_orders, products)\n",
" print_order_statistics(total_products_ordered, percentage_ordered)\n",
" inventory = update_inventory(customer_orders, inventory)\n",
" calculate_total_price(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b06417fc",
"metadata": {},
"outputs": [],
"source": [
"launch_all()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -93,7 +286,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.9"
}
},
"nbformat": 4,
Expand Down