diff --git a/src/control_flow/test_continue.py b/src/control_flow/test_continue.py index 23c015bd..ddaab803 100644 --- a/src/control_flow/test_continue.py +++ b/src/control_flow/test_continue.py @@ -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] diff --git a/src/control_flow/test_for.py b/src/control_flow/test_for.py index 7411277b..9708db34 100644 --- a/src/control_flow/test_for.py +++ b/src/control_flow/test_for.py @@ -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. if len(word) > 6: words.insert(0, word) @@ -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. diff --git a/src/control_flow/test_try.py b/src/control_flow/test_try.py index fed06f59..ba64583d 100644 --- a/src/control_flow/test_try.py +++ b/src/control_flow/test_try.py @@ -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.' diff --git a/src/control_flow/test_while.py b/src/control_flow/test_while.py index 4bc58028..4ce4f02e 100644 --- a/src/control_flow/test_while.py +++ b/src/control_flow/test_while.py @@ -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