-1
isActiveData = isActive["Data"]
    isActiveDataLen = len(isActiveData)

    if isActiveDataLen > 0:
        while isActiveDataLen > 0:
            isActiveDataLen -= 1
            print (isActiveDataLen)

Result

10 9 8 7 6 5 4 3 2 1 0

if isActiveDataLen > 0:
        while isActiveDataLen > 0:
            isActiveDataLen -= 1
            return (isActiveDataLen)

Result

10

I have a program like this. But with print, I can extract the numbers I want from the while loop. When I use return, the while loop only works once and I cannot continue the program. How can I solve the problem?

Elfo
  • 117
  • 1
  • 8
  • you're printing 11 numbers on the first snippet. it's not clear which one of them you want it to return – Nir Alfasi Feb 09 '21 at 12:41
  • I'd advise you to checkout keyword yield. https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do#answers this post explaines it, as well as generators. – Leon Markes Feb 09 '21 at 12:45
  • this is the expected behavior of the `return` statement. What do you want to do with the data? do you want to return all of the fields? – Orozco Feb 09 '21 at 12:46

1 Answers1

0

When you use a return statement, the function stops and returns a value. If you want to extract a given number you can put it in an int for example and then return it at the end of your loop. Or if you want to return multiple values you can return a tuple with all the values you need to return (still at the end of your loop)

lazydev
  • 154
  • 1
  • 1
  • 8
  • Yes, but the data here can be a different value each time a variable function is run. I have tried many times how I can return it as a tuple in this way. – Elfo Feb 09 '21 at 15:24