diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb index 5a3c3e1..fda6f90 100644 --- a/lab-python-list-comprehension.ipynb +++ b/lab-python-list-comprehension.ipynb @@ -75,11 +75,94 @@ "\n", "```\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6d5b9bfc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total of products ordered: 2\n", + "Percentage of products ordered: 40.0%\n", + "Updated Inventory: {'t-shirt': 5, 'mug': 4, 'hat': 2, 'book': 2}\n", + "The total price of your order is: $15.0\n" + ] + } + ], + "source": [ + "# We take our previous code and review it with comprehension sets\n", + "\n", + "# We create a function that initializes the inventory with user input\n", + "def initialize_inventory(products):\n", + " inventory = {product: int(input(f\"Enter the available stock for {product}: \")) for product in products}\n", + " return inventory\n", + "\n", + "# We create a function that asks the user for their order\n", + "def get_customer_orders():\n", + " orders = int(input(\"Please enter the number of different products you want to order: \")) # We ask how many different products the user wants to order because we will iterate that many times\n", + " customer_orders = {input(f\"Enter the product number {i+1} you want to order: \") for i in range(orders)}\n", + " return customer_orders\n", + "\n", + "# We create a function that updates the inventory based on the user order\n", + "def update_inventory(inventory, customer_orders):\n", + " # Decrement the quantity for each ordered product\n", + " inventory = {product: quantity - 1 if product in customer_orders else quantity for product, quantity in inventory.items()}\n", + " # Remove products with zero quantity\n", + " inventory = {product: quantity for product, quantity in inventory.items() if quantity > 0}\n", + " return inventory\n", + "\n", + "# We add a function that calculates the total price of the user order\n", + "def calculate_total_price(customer_orders):\n", + " prices = {product: float(input(f\"Enter the price of {product}: \")) for product in customer_orders}\n", + " total_price = sum(prices[product] for product in customer_orders if product in prices)\n", + " return total_price\n", + "\n", + "# We add a function that prints the total price of the user order\n", + "def print_total_price(total_price):\n", + " print(f\"The total price of your order is: ${total_price:.1f}\")\n", + "\n", + "# We create a function that calculates the user order's statistics\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_of_products_ordered = total_products_ordered / len(products) * 100\n", + " order_statistics = {\n", + " \"Total of products ordered\": total_products_ordered,\n", + " \"Percentage of products ordered\": percentage_of_products_ordered\n", + " }\n", + " return order_statistics\n", + "\n", + "# We create a function that prints the order statistics\n", + "def print_order_statistics(order_statistics):\n", + " print(\"Order Statistics:\")\n", + " print(f\"Total of products ordered: {order_statistics['Total of products ordered']}\")\n", + " print(f\"Percentage of products ordered: {order_statistics['Percentage of products ordered']:.1f}%\")\n", + "\n", + "# We create a function that prints the updated inventory\n", + "def print_updated_inventory(inventory): \n", + " print(\"Updated Inventory: \", inventory)\n", + "\n", + "# Finally we call the functions in the right order to run the program\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders()\n", + "inventory = update_inventory(inventory, customer_orders)\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_statistics)\n", + "print_updated_inventory(inventory)\n", + "total_price = calculate_total_price(customer_orders)\n", + "print_total_price(total_price)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -93,7 +176,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.6" } }, "nbformat": 4,