Skip to content
This repository was archived by the owner on Apr 3, 2026. It is now read-only.

Commit 68e604a

Browse files
authored
feat: Exercise #2. Loops project
BREAKING CHANGE: New exercise added
2 parents 50fd9bb + 5d4f396 commit 68e604a

File tree

4 files changed

+177
-2
lines changed

4 files changed

+177
-2
lines changed

1-basics/exercise.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# 3) Create a function which prints ANY data (two arguments) as one string
99
def print_random_data(data1, data2):
1010
print('The first data is ' + data1 +' and the second data is ' + data2 + '.')
11+
1112
print_random_data('Hello', 'World')
1213

1314
# 4) Create a function which calculates and returns the number of decades you already lived (e.g. 23 = 2 decades)

2-loops/blockchain.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
my_blockchain = []
2+
waiting_for_input = True
3+
4+
def get_transaction_value():
5+
user_input = float(input('Please enter your transaction value: '))
6+
add__line()
7+
return user_input
8+
9+
def take_last_blockchain_value():
10+
# The first line above checks list/array length and ask if is less than 1 (empty list/array)
11+
# In case that statement is true, it will return None, a special data type that represents the absence of a value (like null in javascript)
12+
if len(my_blockchain) < 1:
13+
return None
14+
15+
return my_blockchain[-1]
16+
17+
def add_block(transaction_value, last_transaction):
18+
# To update the logic related to the None value, it will asign a default value of [1] to last_transaction in case its value is abscent (None)
19+
if last_transaction is None:
20+
last_transaction = [1]
21+
22+
my_blockchain.append([last_transaction, transaction_value])
23+
print(my_blockchain)
24+
add__line()
25+
26+
def get_user_input():
27+
user_input = input('Please enter your transaction value: ')
28+
add__line()
29+
return user_input
30+
31+
def return_all_blocks():
32+
print('---Outputting all blocks---')
33+
# A for loop is used to iterate over a list, executing the same block of code for each item in the list
34+
# In this case, the loop will print each block of the blockchain in a different line
35+
for block in my_blockchain:
36+
print('Outputting block: ' + str(block))
37+
add__line()
38+
39+
def verify_chain_by_index():
40+
""" The same function as verify_chain but using the index of the blocks instead of the block itself """
41+
is_valid_chain = True
42+
# There is no index number in a for loop for python, but it can be used through the range() function
43+
for block_index in range(len(my_blockchain)):
44+
if block_index == 0:
45+
# In case you are in the first index, there is no need to check anything, so just continue to the next iteration (continue will skip the iteration and will continue with the next one)
46+
block_index += 1
47+
continue
48+
# The idea in this if is that che the first element of the current block (block[0]) should be equal to the entire previous block (my_blockchain[block_index - 1])
49+
elif my_blockchain[block_index][0] != my_blockchain[block_index - 1]:
50+
is_valid_chain = False
51+
break
52+
53+
block_index += 1
54+
return is_valid_chain
55+
56+
def verify_chain():
57+
""" The function helps to verify the integrity of the blockchain by checking if each block's previous hash matches the hash of the previous block. """
58+
block_index = 0
59+
is_valid_chain = True
60+
61+
for block in my_blockchain:
62+
if block_index == 0:
63+
# In case you are in the first index, there is no need to check anything, so just continue to the next iteration (continue will skip the iteration and will continue with the next one)
64+
block_index += 1
65+
continue
66+
# The idea in this if is that che the first element of the current block (block[0]) should be equal to the entire previous block (my_blockchain[block_index - 1])
67+
elif block[0] != my_blockchain[block_index - 1]:
68+
is_valid_chain = False
69+
break
70+
71+
block_index += 1
72+
return is_valid_chain
73+
74+
def add__line():
75+
print('---------------------')
76+
77+
# A while loop is used to execute a block of code as long as a certain condition is true
78+
# In this case, the true related to the waiting_for_input variable
79+
while waiting_for_input:
80+
add__line()
81+
print('Please choose an option:')
82+
print('1: Add a new block to the blockchain')
83+
print('2: Output all blockchain blocks')
84+
print('h: Manipulate blockchain')
85+
print('q: Quit')
86+
add__line()
87+
88+
user_choice = get_user_input()
89+
90+
# The if else conditional statement gives you the option to execute different blocks of code based on a condition
91+
if user_choice == '1':
92+
input_transaction_value = get_transaction_value()
93+
add_block(input_transaction_value, take_last_blockchain_value())
94+
# In this case, I am using a different condition to execute another block of code called elif (which equals to else if in javascript)
95+
# You can list as many elif conditions as you want between the if and else statements (but there are better and cleaner ways to do this)
96+
elif user_choice == '2':
97+
return_all_blocks()
98+
elif user_choice == 'q':
99+
# Break is used to exit the loop (in this case, to quit the program)
100+
# break
101+
waiting_for_input = False
102+
elif user_choice == 'h':
103+
if len(my_blockchain) >= 1:
104+
my_blockchain[0] = [2.0]
105+
# The final else statement will execute a block of code if none of the previous conditions were met
106+
else:
107+
print('Invalid input, please choose a valid option')
108+
# As a foot note, does not exists such a thing as switch statements in python (sorry about that)
109+
# In this scenario, an not keyword equals '!= value', but is more readable this way
110+
# There are other keywords like 'is' and 'in' that can be used in conditional statements, but it will be seen in other exercises
111+
if not verify_chain_by_index():
112+
print('Invalid blockchain!')
113+
waiting_for_input = False
114+
# The else at the end of the while loop will be executed when the while condition is no longer true
115+
else:
116+
print('User left!')
117+
118+
add__line()
119+
print('Done!')

2-loops/exercise.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
def add__line():
2+
print('--------------------')
3+
4+
# 1) Create a list of names and use a for loop to output the length of each name (len() ).
5+
names_list = ['Nicolas', 'Florencia', 'Omar', 'Lucia', 'Elsie', 'Ruben', 'Emma', 'Juan']
6+
7+
def print_names_length(names):
8+
add__line()
9+
for name in names:
10+
print('The name ' + name + ' has ' + str(len(name)) + ' characters.')
11+
add__line()
12+
13+
print_names_length(names_list)
14+
# 2) Add an if check inside the loop to only output names longer than 5 characters.
15+
16+
def print_long_names_length(names):
17+
add__line()
18+
for name in names:
19+
if len(name) > 5:
20+
print('The name ' + name + ' has ' + str(len(name)) + ' characters.')
21+
add__line()
22+
23+
print_long_names_length(names_list)
24+
# 3) Add another if check to see whether a name includes a “n” or “N” character.
25+
26+
def print_names_with_n(names):
27+
add__line()
28+
for name in names:
29+
if 'n' in name or 'N' in name:
30+
print('The name ' + name + ' contains the letter "n".')
31+
add__line()
32+
33+
print_names_with_n(names_list)
34+
# 4) Use a while loop to empty the list of names (via pop() )
35+
36+
def empty_names_list(names):
37+
add__line()
38+
while len(names) > 0:
39+
print('Removing name: ' + names.pop())
40+
add__line()
41+
print('All names removed!')
42+
43+
empty_names_list(names_list)
44+

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ cd python-practice
3030
```
3131

3232
## How to run it
33-
- To be added
33+
To run any specific exercise, execute the following command in the project´s folder:
34+
```python
35+
python3 exercise.py # Or any .py file
36+
```
3437

3538
## Repo structure & what i learned in each exercise
3639
- Basics (`1-basics` folder)
@@ -40,7 +43,15 @@ cd python-practice
4043
- how to handle `numbers` (integers and float), `strings` and `lists`.
4144
- `functions`, how to handle `default values` and `keyword arguments`.
4245
- variable scope and differences between local and global types.
43-
- using `Doc strings` to document code.
46+
- Using `Doc strings` to document code.
47+
- Loops (`2-loops` folder)
48+
- Understanding how to use `for` loops.
49+
- Also how to handle list index by `range` function.
50+
- Understanding how to use `while` loops.
51+
- Understanding how to use `if/else` and `elif` conditionals.
52+
- Understanding how to use `break` and `continue` words.
53+
- Undesrtanding the usage of `None` value.
54+
- Undesrtanding the `not` keyword.
4455

4556
## Other practice repos
4657
| Node | React | Angular | GraphQL | HTML & CSS | Styling | Typescript | NextJs | Docker |

0 commit comments

Comments
 (0)