From 0e4a6e44ee41f2961571749058d256880f12aa45 Mon Sep 17 00:00:00 2001 From: Ricardo Higginson Date: Sun, 17 May 2026 19:51:10 +0200 Subject: [PATCH] Lab solved --- lab-python-functions.ipynb | 241 ++++++++++++++++++++++++++++++++++++- 1 file changed, 239 insertions(+), 2 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..26dee9c 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,11 +43,248 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "a2157d27", + "metadata": {}, + "outputs": [], + "source": [ + "# 1\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = int(input(\"how many \" + product + \" are there?\"))\n", + " inventory[product] = quantity\n", + " return inventory\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "b93bc9bb", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ec624d05", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 6, 'mug': 5, 'hat': 7, 'book': 6, 'keychain': 4}" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#testing initialize_inventory \n", + "inventory = initialize_inventory(products)\n", + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "2e26d15c", + "metadata": {}, + "outputs": [], + "source": [ + "#2\n", + "def get_customer_orders():\n", + " customer_order = set()\n", + " question = input(\"Would you like to purchase an item (Y/N)?\")\n", + " while question.upper() == 'Y':\n", + " item = input(\"Which item would you like to order? (t-shirt, mug, hat, book, keychain) \")\n", + " if item in inventory:\n", + " customer_order.add(item.lower())\n", + " question = input(\"Would you like to purchase another item? \")\n", + " return customer_order" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "6a3d8ae8", + "metadata": {}, + "outputs": [], + "source": [ + "#testing get_customer_orders and saving it\n", + "customer_order = get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e73b0e09", + "metadata": {}, + "outputs": [], + "source": [ + "# 3\n", + "def update_inventory(customer_order_arg,inventory_arg): \n", + " for item in customer_order_arg:\n", + " inventory_arg[item] = inventory_arg[item] -1\n", + " return inventory\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "07c2fbba", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 6, 'mug': 4, 'hat': 7, 'book': 5, 'keychain': 3}" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#testing update_inventory and updating inventory\n", + "inventory_updated = update_inventory(customer_order, inventory)\n", + "inventory_updated" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bc85db6e", + "metadata": {}, + "outputs": [], + "source": [ + "#4\n", + "def calculate_order_statistics(customer_order_arg, products_arg):\n", + " num_products = len(products_arg)\n", + " num_products_ordered = len(customer_order_arg)\n", + " pct_ordered_products = num_products_ordered / num_products * 100\n", + " return num_products_ordered, pct_ordered_products\n" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "2b3d2079", + "metadata": {}, + "outputs": [], + "source": [ + "order_statistics = calculate_order_statistics(customer_order, products)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ca3ecbd8", + "metadata": {}, + "outputs": [], + "source": [ + "#5\n", + "def print_order_statistics(order_statistics):\n", + " num_products_ordered, pct_ordered_products = order_statistics\n", + " print(f\"The total ordered items are {num_products_ordered} and the percentage of them is {pct_ordered_products}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "c11c605b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The total ordered items are 3 and the percentage of them is 60.0%\n" + ] + } + ], + "source": [ + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e13b76c5", + "metadata": {}, + "outputs": [], + "source": [ + "#6\n", + "def print_updated_inventory(updated_inventory):\n", + " print(updated_inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "c185809b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 6, 'mug': 4, 'hat': 7, 'book': 5, 'keychain': 3}\n" + ] + } + ], + "source": [ + "new_inventory = print_updated_inventory(inventory_updated)\n", + "new_inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5a77fc2f", + "metadata": {}, + "outputs": [], + "source": [ + "#7\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = initialize_inventory(products)\n", + "customer_order = get_customer_orders()\n", + "inventory = update_inventory(inventory, customer_order)\n", + "order_statistics = calculate_order_statistics(customer_order, products)\n", + "print_order_statistics(order_statistics)\n", + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "99379a13", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "64660e8f", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -61,7 +298,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.7" } }, "nbformat": 4,