Skip to content

lab complete #436

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: 123 additions & 0 deletions .ipynb_checkpoints/README-checkpoint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
![logo_ironhack_blue 7](https://user-images.githubusercontent.com/23629340/40541063-a07a0a8a-601a-11e8-91b5-2f13e4e6b441.png)

# LAB | Functions

<details>
<summary>
<h2>Learning Goals</h2>
</summary>

This exercise allows you to practice and apply the concepts and techniques taught in class.

Upon completion of this exercise, you will be able to:

- Define and use functions to modularize code
- Pass arguments to functions and return values from functions
- Call functions from other functions
- Use function encapsulation to control access to variables and functions

<br>
<hr>

</details>

<details>
<summary>
<h2>Prerequisites</h2>
</summary>

Before this starting this lab, you should have learnt about:

- Data types, operators and structures
- Flow control (if-else statements and loops)
- Functions

<br>
<hr>

</details>


## Introduction

In this lab, you will practice one of the most fundamental concepts of programming: functions. Functions are reusable blocks of code that perform specific tasks. They allow you to break up your code into smaller, more manageable pieces, making it easier to read, debug, and maintain.

In this lab, you will learn how to define and call functions, pass arguments and return values. You will also learn how to call functions inside other functions and use encapsulation to hide implementation details.

By the end of this lab, you will have a solid understanding of functions and be able to apply this knowledge to write efficient, reusable, and maintainable code.

**This lab builds upon the solution from the previous exercise and enhances it by incorporating functions to create more robust and improved code.**

Let's get started!

<br>

**Happy coding!** :heart:


## Requirements

- Fork this repo
- Clone it to your machine


## Getting Started

Complete the challenges in the notebook. Follow the instructions and add your code and explanations as necessary.

## Submission

- Upon completion, run the following commands:

```bash
git add .
git commit -m "Solved lab"
git push origin master
```

- Paste the link of your lab in Student Portal.


## FAQs
<details>
<summary>I am stuck in the exercise and don't know how to solve the problem or where to start.</summary>
<br>

If you are stuck in your code and don't know how to solve the problem or where to start, you should take a step back and try to form a clear question about the specific issue you are facing. This will help you narrow down the problem and come up with potential solutions.


For example, is it a concept that you don't understand, or are you receiving an error message that you don't know how to fix? It is usually helpful to try to state the problem as clearly as possible, including any error messages you are receiving. This can help you communicate the issue to others and potentially get help from classmates or online resources.


Once you have a clear understanding of the problem, you will be able to start working toward the solution.

[Back to top](#faqs)

</details>


<details>
<summary>I am unable to push changes to the repository. What should I do?</summary>
<br>

There are a couple of possible reasons why you may be unable to *push* changes to a Git repository:

1. **You have not committed your changes:** Before you can push your changes to the repository, you need to commit them using the `git commit` command. Make sure you have committed your changes and try pushing again. To do this, run the following terminal commands from the project folder:
```bash
git add .
git commit -m "Your commit message"
git push
```
2. **You do not have permission to push to the repository:** If you have cloned the repository directly from the main Ironhack repository without making a *Fork* first, you do not have write access to the repository.
To check which remote repository you have cloned, run the following terminal command from the project folder:
```bash
git remote -v
```
If the link shown is the same as the main Ironhack repository, you will need to fork the repository to your GitHub account first and then clone your fork to your local machine to be able to push the changes.

**Note**: You should make a copy of your local code to avoid losing it in the process.

[Back to top](#faqs)

</details>

191 changes: 191 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e",
"metadata": {},
"source": [
"# Lab | Functions"
]
},
{
"cell_type": "markdown",
"id": "0c581062-8967-4d93-b06e-62833222f930",
"metadata": {
"tags": []
},
"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",
"\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": 12,
"id": "e537341b-93d1-40a0-82b5-bfe535918075",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initialized Inventory: {'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10}\n"
]
}
],
"source": [
"# 1\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\"]\n",
"inventory = initialize_inventory(products)\n",
"\n",
"def initialize_inventory(products):\n",
"\n",
" inventory = {}\n",
" \n",
"\n",
" for product in products:\n",
"\n",
" initial_quantity = 5\n",
" inventory[product] = initial_quantity\n",
"\n",
" return inventory\n",
"print(\"Initialized Inventory:\", inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "bda26377-4556-4f43-b29e-86c680b8f8ea",
"metadata": {},
"outputs": [],
"source": [
"# 2\n",
"def get_customer_orders():\n",
" products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
" customer_orders = set()\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "941f87d7-92c0-4ff9-a822-89764928e234",
"metadata": {},
"outputs": [],
"source": [
"#3 \n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] = max(inventory[product] - 1,0)\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "f8fc2b9a-bf16-4a0a-8bb9-e97ed9d000c4",
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (651772898.py, line 4)",
"output_type": "error",
"traceback": [
"\u001b[0;36m Cell \u001b[0;32mIn[15], line 4\u001b[0;36m\u001b[0m\n\u001b[0;31m percentage ordered = (total_products_orders / len(customer_orders) * 100\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"# 4\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage ordered = (total_products_orders / len(customer_orders) * 100\n",
" return total_products_ordered, percentage_ordered"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "b4c9e544-cec6-4466-9914-e6b9f91e2028",
"metadata": {},
"outputs": [],
"source": [
"# 5\n",
"def print_order_statistics(order_statistics):\n",
" print(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "6dc1df9c-5900-4d6b-84ac-c83126565e4e",
"metadata": {},
"outputs": [],
"source": [
"# 6\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated inventory: \")\n",
" for product, quantity in inventory():\n",
" print(product, quantity)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ceb93022-f275-42d2-87a3-041046c18fd3",
"metadata": {},
"outputs": [],
"source": [
"# 7 \n",
"def ():\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "conda-base-py"
},
"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.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading