From 8c44fd2b064b3cd03809b9f1721977878278f4c9 Mon Sep 17 00:00:00 2001 From: jussara08 Date: Wed, 29 Oct 2025 21:34:03 +0100 Subject: [PATCH] Add files via upload --- lab-python-functions.ipynb | 228 ++++++++++++++++++++++++++++++++++++- 1 file changed, 226 insertions(+), 2 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..39cfda1 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,11 +43,235 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "c57d7eca", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " \n" + ] + } + ], + "source": [ + "\n", + "import builtins\n", + "builtins.print(type(print), print) " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fbd99e86", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " inventory[product] = int(input(f\"How many {product}s do you have in storage?\"))\n", + " return inventory\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d6131b0c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 23, 'mug': 56, 'hat': 49, 'book': 27, 'keychain': 75}\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)\n", + "print(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "184c3b79", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "def get_customer_orders():\n", + " products = []\n", + " still_buying = True\n", + " while still_buying:\n", + " product = input(\"What products would you like to buy?\")\n", + " customer_answer = input(\"Do you wish to buy another product? (Y/N)\")\n", + "\n", + " if customer_answer == \"N\":\n", + " still_buying = False\n", + "\n", + " products.append(product)\n", + " return products\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "7ab0275b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders()\n", + "print(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "b756ee2c", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] = 0\n", + " else:\n", + " print(f\"{product} is not in inventory!\")\n", + " \n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "b80384cc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 0, 'mug': 56, 'hat': 49, 'book': 27, 'keychain': 75}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "inventory = {'t-shirt': 23, 'mug': 56, 'hat': 49, 'book': 27, 'keychain': 75}\n", + "update_inventory(\n", + " customer_orders= customer_orders,\n", + " inventory=inventory \n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "4b9d9ba1", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_ordered = len(customer_orders)\n", + "\n", + " if len(products) > 0:\n", + " percentage_ordered = (len(customer_orders) / len(products)) * 100\n", + " else:\n", + " percentage_ordered = 0\n", + "\n", + " return {\n", + " \"total_ordered\": total_ordered,\n", + " \"percentage_ordered\": percentage_ordered\n", + " }\n" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "7140be35", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Products Ordered: 5\n", + "Percentage of Products Ordered: 100.00%\n" + ] + } + ], + "source": [ + "inventory={'t-shirt': 0, 'mug': 56, 'hat': 49, 'book': 27, 'keychain': 75}\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "customer_orders = {\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"}\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_statistics)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "f06eb63c", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " print(f\"Total Products Ordered: {order_statistics['total_ordered']}\")\n", + " print(f\"Percentage of Products Ordered: {order_statistics['percentage_ordered']:.2f}%\") \n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "5c137855", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 0, 'mug': 56, 'hat': 49, 'book': 27, 'keychain': 75}\n" + ] + } + ], + "source": [ + "print_updated_inventory(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3bcad495", + "metadata": {}, + "outputs": [], + "source": [] } ], "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.13.5" } }, "nbformat": 4,