【问题标题】:Nonetype function which should be an integer (Python)应该是整数的无类型函数(Python)
【发布时间】:2018-09-25 05:26:11
【问题描述】:
a=0

def r(x):
    global a

    if len(str(x))==1:
        print(a)
        b=int(a)
        a=0
        return b
    else:
        a+=1
        print(a)
        r(reduce(lambda z, y: int(z)*int(y), list(str(x))))

def persistence(n):

    if len(str(n))==1:
        return 0
    else:
        return r(n)

(这是 codewars.com 上的挑战)

为什么是type(r(n))==NoneType? 变量 b 是一个整数,那么为什么函数整数的类型也不是呢?

【问题讨论】:

    标签: python function integer typeerror nonetype


    【解决方案1】:

    我相信您的问题是在 else 情况下您没有从函数返回值。所以你应该添加return关键字,如下所示:

    a=0
    
    def r(x):
        global a
    
        if len(str(x))==1:
            print(a)
            b=int(a)
            a=0
            return b
        else:
            a+=1
            print(a)
            return r(reduce(lambda z, y: int(z)*int(y), list(str(x))))
    
    def persistence(n):
    
        if len(str(n))==1:
            return 0
        else:
            return r(n)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 2019-12-30
      • 1970-01-01
      • 1970-01-01
      • 2013-07-28
      • 2020-12-29
      • 2020-04-15
      相关资源
      最近更新 更多