简体   繁体   English

Python 打印是列表,但使用 while 循环返回不是列表

[英]Python print is list but return not list with while loop

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 10 9 8 7 6 5 4 3 2 1 0

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

Result结果

10 10

I have a program like this.我有一个这样的程序。 But with print, I can extract the numbers I want from the while loop.但是通过打印,我可以从 while 循环中提取我想要的数字。 When I use return, the while loop only works once and I cannot continue the program.当我使用 return 时,while 循环只工作一次,我无法继续程序。 How can I solve the problem?我该如何解决这个问题?

When you use a return statement, the function stops and returns a value.当您使用return语句时,function 停止并返回一个值。 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.例如,如果要提取给定数字,可以将其放入int中,然后在循环结束时将其返回。 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)或者,如果您想返回多个值,您可以返回一个包含您需要返回的所有值的元组(仍在循环结束时)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM