diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb index 5a3c3e1..640cbc0 100644 --- a/lab-python-list-comprehension.ipynb +++ b/lab-python-list-comprehension.ipynb @@ -75,11 +75,208 @@ "\n", "```\n" ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "46273357", + "metadata": {}, + "outputs": [], + "source": [ + "#products list\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "dc57dcaf", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products): #defines the starting inventory and its products\n", + " inventory = {}\n", + " print(\"enter quantity for each product:\")\n", + " for item in products:\n", + " quantity = input(f\" {item}: \")\n", + " while not quantity.isdigit():\n", + " print(\"Please enter a valid number.\")\n", + " quantity = input(f\" {item}: \")\n", + " inventory[item] = int(quantity)\n", + " return inventory #returns the initialized inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "ea39718a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "enter quantity for each product:\n", + "Please enter a valid number.\n", + "Initial inventory: {'t-shirt': 89, 'mug': 64, 'hat': 67, 'book': 567, 'keychain': 64}\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)\n", + "print(\"Initial inventory:\", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "edb69c3c", + "metadata": {}, + "outputs": [], + "source": [ + "def get_costumer_orders(products):\n", + " customer_orders = set()\n", + " num_orders = input(\"How many different products would you like to order? \")\n", + " while not num_orders.isdigit() or int(num_orders) <= 0:\n", + " print(\"Please enter a valid positive number.\")\n", + " num_orders = input(\"How many different products would you like to order? \")\n", + " num_orders = int(num_orders)\n", + " for _ in range(num_orders):\n", + " product_name = input(f\"enter the product name: \").lower()\n", + " if product_name in products:\n", + " customer_orders.add(product_name)\n", + " else:\n", + " print(f\"{product_name} is not available.\")\n", + " return customer_orders #returns the set of ordered products" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "ce40dc01", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer orders: {'book', 'hat'}\n" + ] + } + ], + "source": [ + "customer_orders = get_costumer_orders(products)\n", + "print(\"Customer orders:\", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "b6e2fb59", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statitics(customer_orders, products): #calculates the total items ordered and the average items per product\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = (total_products_ordered / len(products)) * 100\n", + " return total_products_ordered, percentage_ordered #returns the total items ordered and the average" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "cdfbc4e6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order statistics: (2, 40.0)\n" + ] + } + ], + "source": [ + "statistics = calculate_order_statitics(customer_orders, products)\n", + "print(\"Order statistics:\", statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "609ad155", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders): #calculates the total price of the ordered products\n", + " total_price = 0.0\n", + " for p in customer_orders:\n", + " price = input(f\"Enter the price for {p}: \")\n", + " while not (price.replace('.','',1).isdigit() and price.count('.') <= 1):\n", + " print(\"Please enter a valid price non negative.\")\n", + " price = input(f\"Enter the price for {p}: \")\n", + " total_price += float(price)\n", + " return total_price #returns the total price of the order" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "ae0128c4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total price of the order: €16.0\n" + ] + } + ], + "source": [ + "total_price = calculate_total_price(customer_orders)\n", + "print(f\"Total price of the order: €{total_price}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "977e8859", + "metadata": {}, + "outputs": [], + "source": [ + "def updated_inventory(inventory, customer_orders): #updates the inventory after an order is placed\n", + " for item in customer_orders:\n", + " if item in inventory and inventory[item] > 0:\n", + " inventory[item] -= 1\n", + " else:\n", + " print(f\"Sorry, {item} is out of stock.\")\n", + " return inventory #returns the updated inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "cd637cba", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated inventory: {'t-shirt': 89, 'mug': 64, 'hat': 66, 'book': 566, 'keychain': 64}\n" + ] + } + ], + "source": [ + "updated_inventory = updated_inventory(inventory, customer_orders)\n", + "print(\"Updated inventory:\", updated_inventory)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -93,7 +290,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,