简体   繁体   中英

I am returning a list from my function, but when I print that returned list, it prints NONE

def f(N, end):
    if end==-1:
        N=[1]+N
        print (N)
        return (N)

    if N[end]!=9:
        N[end]+=1
        return (N)

    if N[end]==9:
        N[end]=0
        end-=1
        print (N)
        f(N,end)    

L=[9,9,9,9,9]
print(f(L, len(L)-1))

You are missing the return clause in the second if statement

  if N[end]!=9:
        N[end]+=1
        return (N)

    if N[end]==9:
        N[end]=0
        end-=1
        print (N)
        return f(N,end) 

By fixing your code like that it should work

你的函数在你的第三个if语句上没有return语句(这就是为什么打印None ,默认情况下你的函数返回None )它只会改变你的列表NL ),如果你打印你的列表L你会看到结果是[0, 0, 0, 0, 0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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