Skip to content

Lab complete #453

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

# LAB | Flow control

<details>
<summary>
<b>Learning Goals</b>
</summary>

<br>

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:

- Use if-else statements to control the flow of execution in a program based on conditions.
- Use loops to repeat actions in a program.
- Combine if-else statements and loops to create more complex programs.
- Apply flow control statements to solve real-world programming problems.

<br>
<hr>

</details>


<details>
<summary>
<b>Prerequisites</b>
</summary>
Before starting this lab, you should have learnt about:

- Data types, operators and structures
- Flow control (if-else statements and loops)
<br>
<hr>

</details>

## Introduction

In this lab, you will practice how to use control structures to control the flow of a program's execution. Control structures are essential to programming as they enable the execution of a program to follow specific paths based on certain conditions. In this lab, you will practice writing code with various control structures, including if-else statements and loops.

By the end of the lab, you should be able to identify situations that require the use of flow control, select the appropriate construct(s) to use, and write code that executes correctly and efficiently. You will also gain practical experience in coding, debugging, and testing programs that use flow control statements. This lab is designed for learners who are familiar with basic Python syntax and data types and are ready to expand their programming skills by learning how to control the flow of their programs.

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

<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>
267 changes: 267 additions & 0 deletions .ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f",
"metadata": {
"tags": []
},
"source": [
"# Lab | Flow Control"
]
},
{
"cell_type": "markdown",
"id": "3851fcd1-cf98-4653-9c89-e003b7ec9400",
"metadata": {},
"source": [
"## Exercise: Managing Customer Orders Optimized\n",
"\n",
"In the last lab, you were starting an online store that sells various products. To ensure smooth operations, you developed a program that manages customer orders and inventory.\n",
"\n",
"You did so without using flow control. Let's go a step further and improve this code.\n",
"\n",
"Follow the steps below to complete the exercise:\n",
"\n",
"1. Look at your code from the lab data structures, and improve repeated code with loops.\n",
"\n",
"2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:\n",
" \n",
" a. Prompt the user to enter the name of a product that a customer wants to order.\n",
" \n",
" b. Add the product name to the \"customer_orders\" set.\n",
" \n",
" c. Ask the user if they want to add another product (yes/no).\n",
" \n",
" d. Continue the loop until the user does not want to add another product.\n",
"\n",
"3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "693516d1-d22f-4348-ac98-7b909720407d",
"metadata": {},
"outputs": [],
"source": [
"#1\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e455eb7a-db74-46c9-af9d-e1fede1b0f4a",
"metadata": {},
"outputs": [],
"source": [
"#2\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "42fa2591-5232-44d3-b9ae-5f37c7f99f2c",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Add an amount for t-shirt 10\n",
"Add an amount for mug 10\n",
"Add an amount for hat 10\n",
"Add an amount for book 10\n",
"Add an amount for keychain 10\n"
]
}
],
"source": [
"#3\n",
"for prod in products:\n",
" #print(prod)\n",
" amount = int(input(f\"Add an amount for {prod}\"))\n",
" inventory[prod] = amount\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "59d0abb5-361e-4ce4-8123-cba6ab5bddb6",
"metadata": {},
"outputs": [],
"source": [
"# 4\n",
"customer_orders=set()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "624794f5-aa54-46c4-9dc3-996d630a2dca",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Add a product (t-shirt, mug, hat, book or keychain) that a customer wants to order: mug\n",
"Do you want to add another product? (yes/no): yes\n",
"Add a product (t-shirt, mug, hat, book or keychain) that you want to order, type no to finish: hat\n",
"Do you want to add another product? (yes/no): yes\n",
"Add a product (t-shirt, mug, hat, book or keychain) that you want to order, type no to finish: book\n",
"Do you want to add another product? (yes/no): no\n"
]
}
],
"source": [
"# 5\n",
"customer_input = input(\"Add a product (t-shirt, mug, hat, book or keychain) that a customer wants to order: \")\n",
"while customer_input != \"no\":\n",
" customer_orders.add(customer_input)\n",
" \n",
" customer_input = input(\"Do you want to add another product? (yes/no): \")\n",
" if customer_input == \"yes\":\n",
" customer_input = input(\"Add a product (t-shirt, mug, hat, book or keychain) that you want to order, type no to finish: \")\n",
" elif customer_input != \"no\":\n",
" break\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "b3f122a3-930c-48d0-8fd6-4f32fee15511",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"customer_order: {'mug', 'book', 'hat'}\n"
]
}
],
"source": [
"#6\n",
"print(f\"customer_order: {customer_orders}\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "9e66443d-410e-4693-8184-4a88a14384d3",
"metadata": {},
"outputs": [],
"source": [
"#7\n",
"\n",
"Total_Products_ordered = len(customer_orders)\n",
"Percentage_of_Products_Ordered = (Total_Products_ordered / 5) * 100\n",
"order_status = (Total_Products_ordered, Percentage_of_Products_Ordered)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "77faf5b9-6e5b-4217-a114-0d56cf25a89b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Orders Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.0 %\n"
]
}
],
"source": [
"#8\n",
"print(\"Orders Statistics:\")\n",
"print(f\"Total Products Ordered: {order_status[0]}\")\n",
"print(f\"Percentage of Products Ordered: {order_status[1]} %\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "794bc579-f6bf-4f3f-a805-0025d2e855ff",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 10, 'mug': 9, 'hat': 9, 'book': 9, 'keychain': 10}\n"
]
}
],
"source": [
"#9 \n",
"for order_prod in customer_orders:\n",
" inventory[order_prod] -= 1\n",
" \n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "cb42abd1-921c-4b59-89a7-d2b77dc4406f",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-shirt: 10\n",
"mug: 9\n",
"hat: 9\n",
"book: 9\n",
"keychain: 10\n"
]
}
],
"source": [
"#10\n",
"for key, value in inventory.items():\n",
" print(f\"{key}: {value}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "512bb063-6753-4e8f-b1cb-ea6c4a4f956b",
"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.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading