From 391ca95fff5f3d80811bc78fa3d8432d0689512c Mon Sep 17 00:00:00 2001 From: LaraibWaheed Date: Thu, 4 Aug 2022 17:20:12 +0500 Subject: [PATCH 1/2] Control Flow practice --- src/control_flow/test_continue.py | 6 +++--- src/control_flow/test_for.py | 9 ++++++--- src/control_flow/test_try.py | 8 +++++--- src/control_flow/test_while.py | 4 ++-- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/control_flow/test_continue.py b/src/control_flow/test_continue.py index 23c015bd..26c18e18 100644 --- a/src/control_flow/test_continue.py +++ b/src/control_flow/test_continue.py @@ -18,12 +18,12 @@ def test_continue_statement(): 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) + 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..0d83a350 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,23 @@ 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 = ' '.join(words) + # pylint: disable=consider-using-enumerate for word_index in range(len(words)): concatenated_string += words[word_index] + ' ' assert concatenated_string == 'Mary had a little lamb ' + assert concatenated_string2 == 'Mary had a little lamb' # Or simply use enumerate(). concatenated_string = '' for word_index, word in enumerate(words): - concatenated_string += word + ' ' + concatenated_string += str(word_index) + ' ' + word + ' ' - assert concatenated_string == 'Mary had a little lamb ' + assert concatenated_string == '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 From b030d3cefa0e71fed4b1e0c42006ef05d60b71d2 Mon Sep 17 00:00:00 2001 From: LaraibWaheed Date: Fri, 5 Aug 2022 20:48:11 +0500 Subject: [PATCH 2/2] Control Flow Correcions --- src/control_flow/test_continue.py | 5 +---- src/control_flow/test_for.py | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/control_flow/test_continue.py b/src/control_flow/test_continue.py index 26c18e18..ddaab803 100644 --- a/src/control_flow/test_continue.py +++ b/src/control_flow/test_continue.py @@ -8,16 +8,13 @@ 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). + # 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. diff --git a/src/control_flow/test_for.py b/src/control_flow/test_for.py index 0d83a350..9708db34 100644 --- a/src/control_flow/test_for.py +++ b/src/control_flow/test_for.py @@ -51,23 +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 = ' '.join(words) - - + 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' + 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 += str(word_index) + ' ' + word + ' ' - - assert concatenated_string == '0 Mary 1 had 2 a 3 little 4 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.