From b37757555c9265878a9a7692508003fc981940bf Mon Sep 17 00:00:00 2001 From: Claire Leyden Date: Wed, 6 May 2026 16:09:29 +0200 Subject: [PATCH] Solved lab --- lab-python-functions.ipynb | 180 ++++++++++++++++++++++++++++++++++++- 1 file changed, 179 insertions(+), 1 deletion(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..9db2549 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,6 +43,184 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "f1d455c4-3936-46e0-b75f-4929e467196f", + "metadata": {}, + "outputs": [], + "source": [ + "#Define a function named initialize_inventory that takes products as a parameter. \n", + "#Inside the function, implement the code for initializing the inventory dictionary using a loop and user input.\n", + "def initialize_inventory(products):\n", + " inventory={}\n", + " for i in range(len(products)):\n", + " inventory[products[i]]=int(input('how many ' + products[i] + 's?'))\n", + " return inventory\n", + "# products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "# inventory=initialize_inventory(products)\n", + "# print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "73b4d3a7-1b65-423f-9e44-cd396b1cc8e4", + "metadata": {}, + "outputs": [], + "source": [ + "#Define a function named get_customer_orders that takes no parameters. Inside the function, implement the code for prompting \n", + "#the user to enter the product names using a loop. \n", + "#The function should return the customer_orders set.\n", + "def get_customer_orders():\n", + " options=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + " customer_orders=set()\n", + " addmore=True\n", + " while addmore==True:\n", + " choice= input('Choose a product from ' + str(options))\n", + " if choice in options:\n", + " customer_orders.add(choice)\n", + " yesno=input('Would you like to add another product? (yes/no)')\n", + " if yesno.lower()=='no':\n", + " addmore=False\n", + " return customer_orders\n", + "#customer_orders=get_customer_orders()\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "6c094bae-cc2d-425a-b905-faefb3ab894f", + "metadata": {}, + "outputs": [], + "source": [ + "#Define a function named update_inventory that takes customer_orders and inventory as parameters. Inside the function, \n", + "#implement the code for updating the inventory dictionary based on the customer orders.\n", + "def update_inventory(customer_orders, inventory):\n", + " for i in customer_orders:\n", + " if inventory[i]!=0 and i in inventory:\n", + " inventory[i]-=1\n", + " return inventory\n", + "# print(inventory)\n", + "# update_inventory(customer_orders, inventory)\n", + "# print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "1a27a04e-10ad-4728-ba1b-f4cd22d81617", + "metadata": {}, + "outputs": [], + "source": [ + "# Define a function named calculate_order_statistics that takes customer_orders and products as parameters. Inside the function, \n", + "# implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). \n", + "# The function should return these values.\n", + "def calculate_order_statistics(customer_orders,products):\n", + " total_number_products=len(customer_orders)\n", + " total=sum(inventory.values())\n", + " percentage_ordered=round(len(customer_orders)/total*100,1)\n", + " #order_status=('Total Products Ordered:',total_number_products,'Percentage of Products Ordered:',percentage_ordered)\n", + " #print('Order Statistics')\n", + " #print(f\"Total Products Ordered: {total_number_products}\")\n", + " #print(f\"Percentage of Products Ordered: {percentage_ordered}%\" )\n", + " order_statistics= [total_number_products, percentage_ordered]\n", + " return order_statistics\n", + "#order_statistics=calculate_order_statistics(customer_orders,products)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "a6cf44c5-c0e0-4f56-8ea6-298d957390da", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics\n", + "Total Products Ordered: 1\n", + "Percentage of Products Ordered: 5.9%\n" + ] + } + ], + "source": [ + "# Define a function named print_order_statistics that takes order_statistics as a parameter. Inside the function, implement the \n", + "# code for printing the order statistics.\n", + "def print_order_statistics(order_statistics):\n", + " total_number_products=order_statistics[0]\n", + " percentage_ordered=order_statistics[1]\n", + " print('Order Statistics')\n", + " print(f\"Total Products Ordered: {total_number_products}\")\n", + " print(f\"Percentage of Products Ordered: {percentage_ordered}%\" )\n", + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "4a7150d7-573d-4168-bf28-7d7225dec467", + "metadata": {}, + "outputs": [], + "source": [ + "# Define a function named print_updated_inventory that takes inventory as a parameter. Inside the function, \n", + "# implement the code for printing the updated inventory.\n", + "def print_updated_inventory(inventory):\n", + " print(inventory)\n", + " for key,value in inventory.items():\n", + " print(f\"There are {value} {key}s\")" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "ebfe0517-662a-45a1-9550-a2023243bc32", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "how many t-shirts? 12\n", + "how many mugs? 12\n", + "how many hats? 12\n", + "how many books? 12\n", + "how many keychains? 12\n", + "Choose a product from ['t-shirt', 'mug', 'hat', 'book', 'keychain'] mug\n", + "Would you like to add another product? (yes/no) hat\n", + "Choose a product from ['t-shirt', 'mug', 'hat', 'book', 'keychain'] no\n", + "Choose a product from ['t-shirt', 'mug', 'hat', 'book', 'keychain'] hat\n", + "Would you like to add another product? (yes/no) no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics\n", + "Total Products Ordered: 2\n", + "Percentage of Products Ordered: 3.4%\n", + "{'t-shirt': 12, 'mug': 11, 'hat': 11, 'book': 12, 'keychain': 12}\n", + "There are 12 t-shirts\n", + "There are 11 mugs\n", + "There are 11 hats\n", + "There are 12 books\n", + "There are 12 keychains\n" + ] + } + ], + "source": [ + "products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory=initialize_inventory(products)\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_updated_inventory(inventory)" + ] } ], "metadata": { @@ -61,7 +239,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,