Skip to content

Lab-python-functions #471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 0 additions & 123 deletions README.md

This file was deleted.

1 change: 1 addition & 0 deletions lab-python-functions (day 2).ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"cells":[{"cell_type":"markdown","id":"25d7736c-ba17-4aff-b6bb-66eba20fbf4e","metadata":{"id":"25d7736c-ba17-4aff-b6bb-66eba20fbf4e"},"source":["# Lab | Functions"]},{"cell_type":"markdown","id":"0c581062-8967-4d93-b06e-62833222f930","metadata":{"tags":[],"id":"0c581062-8967-4d93-b06e-62833222f930"},"source":["## Exercise: Managing Customer Orders with Functions\n","\n","In the previous exercise, you improved the code for managing customer orders by using loops and flow control. Now, let's take it a step further and refactor the code by introducing functions.\n","\n","Follow the steps below to complete the exercise:\n","\n","1. Define a function named `initialize_inventory` that takes `products` as a parameter. Inside the function, implement the code for initializing the inventory dictionary using a loop and user input.\n","\n","2. Define a function named `get_customer_orders` that takes no parameters. Inside the function, implement the code for prompting the user to enter the product names using a loop. The function should return the `customer_orders` set.\n","\n","3. Define a function named `update_inventory` that takes `customer_orders` and `inventory` as parameters. Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n","\n","4. Define a function named `calculate_order_statistics` that takes `customer_orders` and `products` as parameters. Inside the function, implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). The function should return these values.\n","\n","5. Define a function named `print_order_statistics` that takes `order_statistics` as a parameter. Inside the function, implement the code for printing the order statistics.\n","\n","6. Define a function named `print_updated_inventory` that takes `inventory` as a parameter. Inside the function, implement the code for printing the updated inventory.\n","\n","7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n","\n","Hints for functions:\n","\n","- Consider the input parameters required for each function and their return values.\n","- Utilize function parameters and return values to transfer data between functions.\n","- Test your functions individually to ensure they work correctly.\n","\n","\n"]},{"cell_type":"code","execution_count":null,"id":"e413101e","metadata":{"id":"e413101e","outputId":"688e796b-6456-4d19-ec57-4fbfdeb78ebb"},"outputs":[{"name":"stdout","output_type":"stream","text":["{'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n"]}],"source":["products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n","\n","def initialize_inventory(list):\n","\n"," inventory = {}\n","\n"," for key in products:\n"," value = int(input(f\"Value of {key}: \"))\n"," inventory[key] = value\n","\n"," return inventory\n","\n","inventory = initialize_inventory(products)\n","print(inventory)"]},{"cell_type":"code","execution_count":null,"id":"5a0a02cb","metadata":{"id":"5a0a02cb","outputId":"daf8de02-cc2a-4d8f-938a-d4498949c6d1"},"outputs":[{"name":"stdout","output_type":"stream","text":["You choose mug\n","Your lis of products is {'mug'}\n","You choose hat\n","Your lis of products is {'mug', 'hat'}\n","You choose book\n","Your lis of products is {'book', 'mug', 'hat'}\n"]}],"source":["def get_consumer_orders():\n"," trials = 0\n"," customer_orders = set()\n","\n"," while trials<3:\n"," chosen_product = input(f\"Chose one of these products {products}\")\n"," if chosen_product in products:\n"," customer_orders.add(chosen_product)\n"," print(f\"You choose {chosen_product}\")\n"," print(f\"Your lis of products is {customer_orders}\")\n"," trials += 1\n"," else:\n"," print(f\"{chosen_product} is no in the list of products: {products}\")\n","\n"," return customer_orders\n","\n","consumer_orders = get_consumer_orders()"]},{"cell_type":"code","execution_count":null,"id":"14cc3677","metadata":{"id":"14cc3677","outputId":"62c67afa-d7a6-4e3a-f98b-7f8b0a8f7239"},"outputs":[{"data":{"text/plain":["{'book', 'hat', 'mug'}"]},"execution_count":48,"metadata":{},"output_type":"execute_result"}],"source":["consumer_orders"]},{"cell_type":"code","execution_count":null,"id":"29bffa42","metadata":{"id":"29bffa42","outputId":"589519eb-9a38-4056-a5c6-a33df08cdd4d"},"outputs":[{"name":"stdout","output_type":"stream","text":["{'t-shirt': 5, 'mug': 4, 'hat': 4, 'book': 4, 'keychain': 5}\n"]}],"source":["def update_inventory(orders,inv):\n","\n"," for order in orders:\n"," inv[order] -= 1\n","\n"," return inv\n","\n","inventory_updated = update_inventory(consumer_orders,inventory)\n","\n","print(inventory_updated)"]},{"cell_type":"code","execution_count":null,"id":"22c7720b","metadata":{"id":"22c7720b","outputId":"311d17a2-ffbe-4700-c947-a100e2a38e25"},"outputs":[{"data":{"text/plain":["'You order 3 products and the percentage of products ordered is 60.0%'"]},"execution_count":50,"metadata":{},"output_type":"execute_result"}],"source":["def calculate_order_statistics(customer_orders,products):\n","\n"," orders = len(customer_orders)\n","\n"," percentage = orders/len(products) * 100\n","\n"," return f\"You order {orders} products and the percentage of products ordered is {percentage}%\"\n","\n","calculate_order_statistics(consumer_orders,products)"]},{"cell_type":"code","execution_count":null,"id":"a835538c","metadata":{"id":"a835538c","outputId":"111161e9-dff1-46db-ac80-2902d7083ee4"},"outputs":[{"name":"stdout","output_type":"stream","text":["{'t-shirt': 5, 'mug': 4, 'hat': 4, 'book': 4, 'keychain': 5}\n"]}],"source":["def print_updated_invetory(inventory):\n","\n"," print(inventory)\n","\n","print_updated_invetory(inventory_updated)\n","\n"]}],"metadata":{"kernelspec":{"display_name":"Python 3","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.13.0"},"colab":{"provenance":[]}},"nbformat":4,"nbformat_minor":5}
69 changes: 0 additions & 69 deletions lab-python-functions.ipynb

This file was deleted.