首页 > 解决方案 > 返回 Null 类型数据的 Python 函数

问题描述

我正在尝试计算给定值的 'nth Fibonacci % m '。(使用 pisano 的系列)。这是终端正在显示的消息。

错误信息->

 100 2
    Traceback (most recent call last):
      File "fibag.py", line 27, in <module>
        print(huge_fibo(n,m))
      File "fibag.py", line 21, in huge_fibo
        return get_fibo(rem) % m
    TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'



#uses python3
def pisano_len(m):
    prev=0
    next=1
    for i in range(m*m+1):
        prev,next=next,(prev+next)%m
        if(prev==0 and next==1):
            return i+1
def get_fibo(n):
    if(n<1):
        return n
    prev=0
    curr=1
    for i in range(n-1):
        (prev,curr)=(curr,prev+curr)
        return curr

def huge_fibo(n,m):
    rem=int(n%pisano_len(m))
    return get_fibo(rem) % m

if(__name__=='__main__'):


    n,m=map(int,input().split())
    print(huge_fibo(n,m))

想不通原因。

标签: python-3.xtypeerrorfibonaccinonetype

解决方案


您的get_fibo()函数不执行 return 语句 if n == 1,有效地返回None


推荐阅读