diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/.DS_Store differ diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..7f5b460 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,13 +43,269 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "103f13f3", + "metadata": {}, + "outputs": [], + "source": [ + "#Define a list\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "#1.Define a function named initialize_inventory that takes products as a parameter\n", + "def initialize_inventory(products):\n", + " #Implement the code for initializing an inventory dictionary using a loop and user input\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = input(f\"Enter the quantity for {product}: \")\n", + " while not quantity.isdigit():\n", + " quantity = input(f\"Invalid input. Please enter a valid non-negative number for {product}: \")\n", + " inventory[product] = int(quantity)\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "95662e43", + "metadata": {}, + "outputs": [], + "source": [ + "#2.Define a function named get_customer_orders that takes no parameters\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "def get_customer_orders():\n", + " #Implement the code for prompting the user to enter the products names usign a loop\n", + " orders = []\n", + " done = False \n", + " products_lower = [p.lower() for p in products]\n", + " while not done:\n", + " product = input(\"Enter a product name (or 'done' to finish): \").strip().lower()\n", + " if product == 'done':\n", + " done = True\n", + " elif product == \" \":\n", + " print(\"Product name cannot be empty. Please try again.\")\n", + " elif product not in products_lower:\n", + " print(f\"Invalid product. Please choose from: {', '.join(products)}\")\n", + " else:\n", + " original_product = products[products_lower.index(product)]\n", + " orders.append(original_product)\n", + " #Should return the customer_orders set\n", + " return set(orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "id": "81eb6417", + "metadata": {}, + "outputs": [], + "source": [ + "#3.Define a function named update_inventory that takes customer_orders and inventory as parameters\n", + "def update_inventory(customer_orders, inventory):\n", + " #Implement the code for updating the inventory based on customer orders\n", + " for order in customer_orders:\n", + " if order in inventory:\n", + " if inventory[order] > 0:\n", + " inventory[order] -= 1\n", + " print(f\"Order for {order} processed. Remaining quantity: {inventory[order]}\")\n", + " else:\n", + " print(f\"Sorry, {order} is out of stock.\")\n", + " else:\n", + " print(f\"{order} is not available in the inventory.\")\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "id": "2e54e87a", + "metadata": {}, + "outputs": [], + "source": [ + "#4. Define a function named calculate_order_statistics that takes customer_orders and products as parameters\n", + "def calculate_order_statistics(customer_orders, products):\n", + " #Implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered)\n", + " total_orders = len(customer_orders)\n", + " unique_products = set(customer_orders)\n", + " total_products = len(products)\n", + " percentage_unique = (len(unique_products) / total_products) * 100 if total_products > 0 else 0\n", + " return total_orders, percentage_unique" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "id": "8cd98b3a", + "metadata": {}, + "outputs": [], + "source": [ + "#5. Define a function named print_order_statistics that takes customer_orders and products as parameters\n", + "def print_order_statistics(customer_orders, products):\n", + " #Implement the code for printing the order statistics\n", + " total_orders, percentage_unique = calculate_order_statistics(customer_orders, products)\n", + " print(f\"Total products ordered: {total_orders}\")\n", + " print(f\"Percentage of unique products ordered: {percentage_unique:.2f}%\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "id": "1754c830", + "metadata": {}, + "outputs": [], + "source": [ + "#6. Define a function named print_updated_inventory that takes inventory as a parameter\n", + "def print_updated_inventory(inventory):\n", + " #Implement the code for printing the updated inventory\n", + " print(\"Updated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity} units\")" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "id": "e2ed4623", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Welcome to the inventory management system!\n", + "\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity for t-shirt: 3\n", + "Enter the quantity for mug: 4\n", + "Enter the quantity for hat: 5\n", + "Enter the quantity for book: 6\n", + "Enter the quantity for keychain: 7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Inventory initialized.\n", + "\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product name (or 'done' to finish): mug\n", + "Enter a product name (or 'done' to finish): jeans\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product. Please choose from: t-shirt, mug, hat, book, keychain\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product name (or 'done' to finish): booik\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product. Please choose from: t-shirt, mug, hat, book, keychain\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product name (or 'done' to finish): book\n", + "Enter a product name (or 'done' to finish): Book\n", + "Enter a product name (or 'done' to finish): BOOK\n", + "Enter a product name (or 'done' to finish): 345\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product. Please choose from: t-shirt, mug, hat, book, keychain\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product name (or 'done' to finish): MUG\n", + "Enter a product name (or 'done' to finish): DONE\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order for book processed. Remaining quantity: 5\n", + "Order for mug processed. Remaining quantity: 3\n", + "Total products ordered: 2\n", + "Percentage of unique products ordered: 40.00%\n", + "Updated Inventory:\n", + "t-shirt: 3 units\n", + "mug: 3 units\n", + "hat: 5 units\n", + "book: 5 units\n", + "keychain: 7 units\n", + "\n", + "Thank you for using the inventory management system!\n" + ] + } + ], + "source": [ + "#7.Call the functions in the appropriate sequence to execute the program and manage customer orders\n", + "def main():\n", + " print(\"\\nWelcome to the inventory management system!\\n\")\n", + " inventory = initialize_inventory(products)\n", + " print(\"\\nInventory initialized.\\n\")\n", + "\n", + " customer_orders = get_customer_orders()\n", + " updated_inventory = update_inventory(customer_orders, inventory)\n", + " \n", + " print_order_statistics(customer_orders, products)\n", + " print_updated_inventory(updated_inventory)\n", + " \n", + " print(\"\\nThank you for using the inventory management system!\")\n", + "\n", + "if __name__ == \"__main__\":\n", + " main()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "14deb3c7-18a7-4821-a374-4c04156b977a", + "metadata": {}, + "outputs": [], + "source": [] } ], "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 +317,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,