From da4e3184dfa3d38b172a8c24f2dac24c781b830e Mon Sep 17 00:00:00 2001 From: Ricardo Castanheira Date: Thu, 4 Sep 2025 15:52:44 +0100 Subject: [PATCH] Solved lab --- lab-python-functions.ipynb | 133 ++++++++++++++++++++++++++++++++++++- 1 file changed, 130 insertions(+), 3 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..de43689 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,13 +43,140 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "a87f8b63-0d1c-4b18-a416-c708fb0ed57f", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the quantity of items in the inventory for product t-shirt: 5\n", + "Please enter the quantity of items in the inventory for product mug: 5\n", + "Please enter the quantity of items in the inventory for product hat: 5\n", + "Please enter the quantity of items in the inventory for product book: 5\n", + "Please enter the quantity of items in the inventory for product keychain: 5\n" + ] + }, + { + "ename": "NameError", + "evalue": "name 'inventory' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[6], line 90\u001b[0m\n\u001b[1;32m 87\u001b[0m \u001b[38;5;66;03m# 7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\u001b[39;00m\n\u001b[1;32m 89\u001b[0m start \u001b[38;5;241m=\u001b[39m initialize_inventory(products)\n\u001b[0;32m---> 90\u001b[0m get_cust_orders \u001b[38;5;241m=\u001b[39m get_customer_orders()\n\u001b[1;32m 91\u001b[0m new_inv \u001b[38;5;241m=\u001b[39m update_inventory(customer_orders, inventory)\n\u001b[1;32m 92\u001b[0m stats \u001b[38;5;241m=\u001b[39m calculate_order_statistics(customer_orders, products)\n", + "Cell \u001b[0;32mIn[6], line 28\u001b[0m, in \u001b[0;36mget_customer_orders\u001b[0;34m()\u001b[0m\n\u001b[1;32m 22\u001b[0m customer_orders \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mset\u001b[39m()\n\u001b[1;32m 24\u001b[0m \u001b[38;5;66;03m# new_order = input(\"Please enter product you would like to order: \").lower()\u001b[39;00m\n\u001b[1;32m 25\u001b[0m \u001b[38;5;66;03m# customer_orders.add(new_order)\u001b[39;00m\n\u001b[1;32m 26\u001b[0m \u001b[38;5;66;03m# query = input(\"Do you want to order another product? Yes/No: \").lower()\u001b[39;00m\n\u001b[0;32m---> 28\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m product \u001b[38;5;129;01min\u001b[39;00m inventory\u001b[38;5;241m.\u001b[39mkeys():\n\u001b[1;32m 30\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m query \u001b[38;5;241m!=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mno\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m 31\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m query \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myes\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n", + "\u001b[0;31mNameError\u001b[0m: name 'inventory' is not defined" + ] + } + ], + "source": [ + "# 1. Define a function named initialize_inventory that takes products as a parameter. \n", + "# Inside the function, implement the code for initializing the inventory dictionary\n", + "# using a loop and user input.\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "def initialize_inventory(products):\n", + "\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = int(input(f\"Please enter the quantity of items in the inventory for product {product}: \"))\n", + " inventory[product] = quantity \n", + " \n", + " return inventory\n", + "\n", + "# 2. Define a function named get_customer_orders that takes no parameters.\n", + "# Inside the function, implement the code for prompting the user to enter the product names using a loop.\n", + "# The function should return the customer_orders set.\n", + "\n", + "def get_customer_orders():\n", + "\n", + " customer_orders = set()\n", + "\n", + "# new_order = input(\"Please enter product you would like to order: \").lower()\n", + "# customer_orders.add(new_order)\n", + "# query = input(\"Do you want to order another product? Yes/No: \").lower()\n", + " \n", + " for product in inventory.keys():\n", + " \n", + " while query != \"no\":\n", + " if query == \"yes\":\n", + " answer = int(input(f\"Would you like to buy product {product}?: \"))\n", + " if answer == \"yes\":\n", + " customer_orders.add(product)\n", + " query = input(\"Do you want to order another product? Yes/No: \").lower()\n", + " else:\n", + " print(\"Invalid Input. Valid options are Yes or No.\")\n", + " query = input(\"Do you want to order another product? Yes/No: \").lower()\n", + "\n", + " return customer_orders\n", + "\n", + "# 3. Define a function named update_inventory that takes customer_orders and inventory as parameters.\n", + "# 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", + "\n", + " for product in customer_orders:\n", + " if inventory[product] > 0:\n", + " inventory[product] -= 1 \n", + "\n", + " return inventory \n", + "\n", + "# 4. Define a function named calculate_order_statistics that takes customer_orders and products as parameters. \n", + "# Inside the function, 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", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + "\n", + " total_products_ordered = len(customer_orders)\n", + " percentage = (total_products_ordered / len(products)) * 100\n", + "\n", + " statistics = {\"Total products ordered\": total_products_ordered,\n", + " \"Percentage of unique products\": percentage\n", + " }\n", + " \n", + " return statistics\n", + " \n", + " \n", + "# 5. Define a function named print_order_statistics that takes order_statistics as a parameter. \n", + "# Inside the function, implement the code for printing the order statistics.\n", + "\n", + "def print_order_statistics(order_statistics):\n", + "\n", + " order_statistics = {\"Statistics:\": statistics}\n", + "\n", + " return order_statistics\n", + "\n", + "# 6. Define a function named print_updated_inventory that takes inventory as a parameter. \n", + "# Inside the function, implement the code for printing the updated inventory.\n", + "\n", + "def print_updated_inventory(inventory):\n", + "\n", + " inventory = {\"Inventory\": inventory}\n", + "\n", + " return inventory\n", + "\n", + "# 7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n", + "\n", + "start = initialize_inventory(products)\n", + "get_cust_orders = get_customer_orders()\n", + "new_inv = update_inventory(customer_orders, inventory)\n", + "stats = calculate_order_statistics(customer_orders, products)\n", + "show = print_order_statistics(order_statistics)\n", + "up_inv = print_updated_inventory(inventory)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -61,7 +188,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,