Search code examples
pythonpython-3.xautomation

Why in this program print None in output?


Why in this program print (None) at the end of output?

def skip_elements(elements):

    for index, item in enumerate(elements):
        if index % 2 != 1:
            print(item, end=(" "))
    

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

Solution

  • You need to remove the print when you're calling your fuction since you're already printing the result inside it.

    Code:

    def skip_elements(elements):
    
        for index in range(len(elements)):
            if index % 2 != 1:
                print(elements[index],end=" ")
                
    skip_elements(["a", "b", "c", "d", "e", "f","g"])
    skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])
    

    Output:

    a c e g Orange Strawberry Peach