0
def skip_elements(elements):
    new_list = []
    i = 0

    for a in elements:
        print(a)
        if i%2==0:
            new_list+=a
        i+=1

    return new_list

print(skip_elements(["a", "b", "c", "d", "e", "f", "g"])) # Should be ['a', 'c', 'e', 'g']
print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])) # Should be ['Orange', 'Strawberry', 'Peach']
print(skip_elements([])) # Should be []

This is Showing indentation error.Even this doesn't work when we place print(a) just before i+=1, whereas the below code works perfectly.Why so??

def skip_elements(elements):
    new_list = []
    i = 0

    for a in elements:
        if i%2==0:
            new_list+=a
        i+=1
        print(a)

    return new_list

print(skip_elements(["a", "b", "c", "d", "e", "f", "g"])) # Should be ['a', 'c', 'e', 'g']
print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])) # Should be ['Orange', 'Strawberry', 'Peach']
print(skip_elements([])) # Should be []

2 Answers2

2

This is a case of using both tabs and spaces in the code, You can use an editor like Notepad++ to see the inconsistencies or and IDE like Pycharm to debug these issues easily. In Pycharm you can reformat your code by ctrl + shift + alt + l in Windows/Linux and cmd + shift + option + l in MacOS.

Shreyansh
  • 458
  • 4
  • 11
-2

Here should be :

new_list.append(a)

not:

new_list+=a

then result

DYZ
  • 55,249
  • 10
  • 64
  • 93
Yuan Yang
  • 1
  • 1
  • Do not include screenshots in either questions or answers. Also, while you raised a valid concern, it does not address the OP's question. – DYZ May 28 '20 at 07:24