Skip to content
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
11 changes: 4 additions & 7 deletions src/control_flow/test_continue.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,19 @@

def test_continue_statement():
"""CONTINUE statement in FOR loop"""

# Let's

# This list will contain only even numbers from the range.
even_numbers = []
# This list will contain every other numbers (in this case - ods).
rest_of_the_numbers = []

for number in range(0, 10):
# Check if remainder after division is zero (which would mean that number is even).
if number % 2 == 0:
even_numbers.append(number)
# Check if remainder after division is not zero (which would mean that number is odd).
if number % 2:
rest_of_the_numbers.append(number)
# Stop current loop iteration and go to the next one immediately.
continue

rest_of_the_numbers.append(number)
even_numbers.append(number)

assert even_numbers == [0, 2, 4, 6, 8]
assert rest_of_the_numbers == [1, 3, 5, 7, 9]
21 changes: 15 additions & 6 deletions src/control_flow/test_for.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_for_statement():
# (for example to duplicate selected items), it is recommended that you first make a copy.
# Iterating over a sequence does not implicitly make a copy. The slice notation makes this
# especially convenient:
for word in words[:]: # Loop over a slice copy of the entire list.
for word in words.copy(): # Loop over a slice copy of the entire list.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for changing this?

if len(word) > 6:
words.insert(0, word)

Expand All @@ -51,20 +51,29 @@ def test_for_statement():
# To iterate over the indices of a sequence, you can combine range() and len() as follows:
words = ['Mary', 'had', 'a', 'little', 'lamb']
concatenated_string = ''

concatenated_string2 = ''
# pylint: disable=consider-using-enumerate
for word_index in range(len(words)):
concatenated_string += words[word_index] + ' '
for word in words:
concatenated_string2 += word + ' '

assert concatenated_string == 'Mary had a little lamb '
assert concatenated_string2 == 'Mary had a little lamb '

# Or simply use enumerate().
concatenated_string = ''
string_dictionary={}

for word_index, word in enumerate(words):
concatenated_string += word + ' '

assert concatenated_string == 'Mary had a little lamb '
string_dictionary[word_index] = word

assert string_dictionary == {
0 : 'Mary',
1 : 'had',
2 : 'a',
3 : 'little',
4 : 'lamb'
}

# When looping through dictionaries, the key and corresponding value can be retrieved at the
# same time using the items() method.
Expand Down
8 changes: 5 additions & 3 deletions src/control_flow/test_try.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,24 @@ def test_try():
# pylint: disable=broad-except
try:
message += 'Success.'
print(non_existing_variable)
except NameError:
message += 'Something went wrong.'
else:
message += 'Nothing went wrong.'

assert message == 'Success.Nothing went wrong.'
assert message == 'Success.Something went wrong.'

# The finally block, if specified, will be executed regardless if the try block raises an
# error or not.
message = ''
try:
# pylint: undefined-variable
print(not_existing_variable) # noqa: F821
# print(not_existing_variable) # noqa: F821
message += 'No error.'
except NameError:
message += 'Something went wrong.'
finally:
message += 'The "try except" is finished.'

assert message == 'Something went wrong.The "try except" is finished.'
assert message == 'No error.The "try except" is finished.'
4 changes: 2 additions & 2 deletions src/control_flow/test_while.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def test_while_statement():

result = 1

while power > 0:
while power >= 0:
result *= number
power -= 1

# 2^5 = 32
assert result == 32
assert result == 64