Function with python repeat loop

Asked

Viewed 126 times

-1

Hello,

I’m trying to use a function to get the full list of status-independent containers, "Docker ps-a". When I use code outside of function, it works. inserir a descrição da imagem aqui

But when I use the same code inside the function, it only returns me the first element of the list. inserir a descrição da imagem aqui

Why does this happen ?

  • 1

    Fabiano do not use image, edit the question and put the code, doing so you help anyone to help you.

1 answer

0


This happens because a return will always be an end of a function.

You can have infinities return, at the first touch your function will be finished and will return the object that Return is pointing to.

Example:

Function that prints a list.

In both cases they will receive as input:

[1,2,3,4]

Below two cases, the last one being the one you are using.

def printar(n):
    for x in n:
        print(x)

This would be a perfect skeleton for your code. For the result would be:

1
2
3
4

However, what you’re trying to do is:

def printar(n):
    for x in n:
        return(x)

That the result would be:

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.