diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..aec20bf 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,11 +43,235 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 58, + "id": "a4441357", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['mug', 'hat', 'book']\n", + "{'mug': 10, 'hat': 20, 'book': 30}\n" + ] + } + ], + "source": [ + "product = input('Enter the name of product: ')\n", + "product_list = []\n", + "\n", + "while product: \n", + " product_list.append(product)\n", + " request = input ('Do you want to add another product: Yes / No: ')\n", + " request = request.lower()\n", + " if request == 'yes':\n", + " product = input('Enter the name of product:')\n", + " else:\n", + " break\n", + "\n", + "print(product_list)\n", + "\n", + "def initialize_inventory(products):\n", + " result = {}\n", + " for product in products:\n", + " unit = int(input('Enter the unit of' + ' ' + product))\n", + " result[product] = unit\n", + " \n", + " return result\n", + "\n", + " \n", + "\n", + "inventory_dic = initialize_inventory(product_list) \n", + "print (inventory_dic)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "id": "871f2d8d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'book', 'hat'}\n" + ] + } + ], + "source": [ + "def get_customer_orders():\n", + " result = set()\n", + " product = input('Enter the name of product: ')\n", + " while product: \n", + " result.add(product)\n", + " request = input ('Do you want to add another product: Yes / No: ')\n", + " request = request.lower()\n", + " if request == 'yes':\n", + " product = input('Enter the name of product:')\n", + " else:\n", + " break\n", + "\n", + " return (result)\n", + " \n", + "customer_orders = get_customer_orders()\n", + "print(customer_orders)\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "id": "d073db7c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug': 10, 'hat': 18, 'book': 28}\n" + ] + } + ], + "source": [ + "## 3. Define a function named `update_inventory` that takes `customer_orders` and `inventory` as parameters. Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " for order in customer_orders:\n", + " if order in inventory:\n", + " inventory[order] = inventory[order] - 1\n", + " \n", + " return inventory\n", + "\n", + "updated_inventory = update_inventory (customer_orders, inventory_dic)\n", + "print(updated_inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "id": "fdc24099", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 66.66666666666666)\n" + ] + } + ], + "source": [ + "## 4. Define a function named `calculate_order_statistics` that takes `customer_orders` and `products` as parameters. Inside the function, implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). The function should return these values.\n", + "\n", + "def calculate_order_statistics (customer_orders, products):\n", + " total_order = len(customer_orders)\n", + " percentage = total_order / len(products) * 100\n", + " return(total_order, percentage)\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, product_list)\n", + "print(order_statistics)\n", + "\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "id": "2b03ec8d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total order: 2\n", + "Percentage 66.66666666666666\n" + ] + } + ], + "source": [ + "##5. Define a function named `print_order_statistics` that takes `order_statistics` as a parameter. Inside the function, implement the code for printing the order statistics.\n", + "\n", + "def print_order_statistics(order_statistics): \n", + " print('Total order:', order_statistics[0])\n", + " print('Percentage', order_statistics[1] )\n", + "\n", + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "id": "6413b752", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mug 10\n", + "hat 18\n", + "book 28\n" + ] + } + ], + "source": [ + "##6. Define a function named `print_updated_inventory` that takes `inventory` as a parameter. Inside the function, implement the code for printing the updated inventory.\n", + "\n", + "def print_updated_inventory (inventory): \n", + " for name in inventory:\n", + " print(name, inventory[name])\n", + "\n", + "print_updated_inventory(updated_inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "id": "51e905a0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug': 33, 'hat': 56, 'book': 22}\n", + "{'book', 'hat'}\n", + "{'mug': 33, 'hat': 55, 'book': 21}\n", + "(2, 66.66666666666666)\n", + "Total order: 2\n", + "Percentage 66.66666666666666\n", + "mug 33\n", + "hat 55\n", + "book 21\n" + ] + } + ], + "source": [ + "##7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n", + "\n", + "inventory_dic = initialize_inventory(product_list) \n", + "print (inventory_dic)\n", + "customer_orders = get_customer_orders()\n", + "print(customer_orders)\n", + "updated_inventory = update_inventory (customer_orders, inventory_dic)\n", + "print(updated_inventory)\n", + "order_statistics = calculate_order_statistics(customer_orders, product_list)\n", + "print(order_statistics)\n", + "print_order_statistics(order_statistics)\n", + "print_updated_inventory(updated_inventory)\n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -61,7 +285,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,