From b93ac2c5845ba742a1e56d0b919d3d8f510c4276 Mon Sep 17 00:00:00 2001 From: Yilak Kebede Date: Fri, 26 Sep 2025 20:43:01 +0200 Subject: [PATCH] Add Lab Functions exercise --- Lab_functions.ipynb | 233 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 233 insertions(+) create mode 100644 Lab_functions.ipynb diff --git a/Lab_functions.ipynb b/Lab_functions.ipynb new file mode 100644 index 0000000..4f6fdbe --- /dev/null +++ b/Lab_functions.ipynb @@ -0,0 +1,233 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "770f1bcf-c1cb-4aa5-9d05-f0c99b63afdc", + "metadata": {}, + "source": [ + "### Creating functions " + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "977a6139-e896-4f60-b1ce-c14e354e85b4", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 1: Initialize inventory\n", + "def initialize_inventory(products):\n", + " \"\"\"\n", + " Initialize inventory dictionary with product names and stock quantities.\n", + " Args:\n", + " products (list): List of product names\n", + " Returns:\n", + " dict: Inventory with product as key and quantity as value\n", + " \"\"\"\n", + " inventory = {}\n", + " for product in products:\n", + " qty = int(input(f\"Enter stock quantity for {product}: \"))\n", + " inventory[product] = qty\n", + " return inventory\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "7d1d4970-0e90-40af-8143-c5ea05f846c7", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 2: Get customer orders\n", + "def get_customer_orders():\n", + " \"\"\"\n", + " Ask the customer to enter products they want to order.\n", + " Returns:\n", + " set: Unique customer orders\n", + " \"\"\"\n", + " customer_orders = set()\n", + " while True:\n", + " order = input(\"Enter a product name (or 'done' to finish): \").strip().lower()\n", + " if order == \"done\":\n", + " break\n", + " customer_orders.add(order)\n", + " return customer_orders\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "7fb9ef5e-fdcb-4579-a073-4bd016d41e04", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 3: Update inventory\n", + "def update_inventory(customer_orders, inventory):\n", + " \"\"\"\n", + " Decrease inventory stock for each ordered product.\n", + " Args:\n", + " customer_orders (set)\n", + " inventory (dict)\n", + " \"\"\"\n", + " for product in customer_orders:\n", + " if product in inventory and inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " else:\n", + " print(f\" {product} is not available or out of stock!\")\n", + " return inventory\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "a43725f5-7241-46fe-8b40-6eab3edae036", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 4: Calculate order statistics\n", + "def calculate_order_statistics(customer_orders, products):\n", + " \"\"\"\n", + " Calculate total ordered items and percentage of unique products ordered.\n", + " Args:\n", + " customer_orders (set)\n", + " products (list)\n", + " Returns:\n", + " tuple: (total_orders, percentage_unique)\n", + " \"\"\"\n", + " total_orders = len(customer_orders)\n", + " percentage_unique = (total_orders / len(products)) * 100\n", + " return total_orders, percentage_unique" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "64a57760-3769-4394-b5f2-f08b6a2ff730", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 5: Print order statistics\n", + "def print_order_statistics(order_statistics):\n", + " \"\"\"\n", + " Print total orders and percentage of unique products ordered.\n", + " \"\"\"\n", + " total_orders, percentage_unique = order_statistics\n", + " print(f\"Total unique products ordered: {total_orders}\")\n", + " print(f\"Percentage of catalog ordered: {percentage_unique:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "4a0d5118-016e-4de5-81ca-f96426c2e782", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 6: Print updated inventory\n", + "def print_updated_inventory(inventory):\n", + " \"\"\"\n", + " Print remaining stock in inventory.\n", + " \"\"\"\n", + " print(\"\\nUpdated Inventory:\")\n", + " for product, qty in inventory.items():\n", + " print(f\"{product}: {qty}\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "bb094ed2-3292-41bc-b093-01e6c9e24fce", + "metadata": {}, + "source": [ + "### Main program excution " + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "0ed84443-0fdf-4a9c-be65-bb299609766e", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter stock quantity for apple: 3\n", + "Enter stock quantity for banana: 3\n", + "Enter stock quantity for orange: 4\n", + "Enter stock quantity for mango: 5\n", + "Enter a product name (or 'done' to finish): 3\n", + "Enter a product name (or 'done' to finish): done\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + " 3 is not available or out of stock!\n", + "Total unique products ordered: 1\n", + "Percentage of catalog ordered: 25.00%\n", + "\n", + "Updated Inventory:\n", + "apple: 3\n", + "banana: 3\n", + "orange: 4\n", + "mango: 5\n" + ] + } + ], + "source": [ + "# Main program execution\n", + "# -------------------------------\n", + "if __name__ == \"__main__\":\n", + " # Product catalog\n", + " products = [\"apple\", \"banana\", \"orange\", \"mango\"]\n", + "\n", + " # Initialize inventory\n", + " inventory = initialize_inventory(products)\n", + "\n", + " # Get customer orders\n", + " customer_orders = get_customer_orders()\n", + "\n", + " # Update inventory\n", + " inventory = update_inventory(customer_orders, inventory)\n", + "\n", + " # Calculate statistics\n", + " order_stats = calculate_order_statistics(customer_orders, products)\n", + "\n", + " # Print results\n", + " print_order_statistics(order_stats)\n", + " print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "50a48791-fc50-4681-a791-11dcbd39cf90", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}