Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 斐波那契函数中的类型错误_Python - Fatal编程技术网

Python 斐波那契函数中的类型错误

Python 斐波那契函数中的类型错误,python,Python,当我运行代码时,它会返回以下错误: def Fastfib(n, memo): global numcalls numcalls += n print ('fib called with', n) if not n in memo: memo[n] = fastFib(n-1, memo) + fastFib(n-2, memo) return memo(n) numcalls = 0 n = 6 res = Fastfib(n) p

当我运行代码时,它会返回以下错误:

def Fastfib(n, memo):
    global numcalls
    numcalls += n
    print ('fib called with', n)
    if not n in memo:
        memo[n] = fastFib(n-1, memo) + fastFib(n-2, memo)   
    return memo(n)
numcalls = 0
n = 6
res = Fastfib(n)
print ('fib of', n, '*', res, 'numcalls *', numcalls,)

定义函数时,指定参数,其中一些参数只有在标记为参数时才是可选的。在您的情况下,这两个参数都不是可选的,因此您必须调用提供它们的方法


检查代码很明显,第二个参数是一个列表。您还应该检查您的算法,它是不完整的,可能会导致许多错误,因为您没有在内存列表中存储元素n-1和n-2FastFib也表示,梅蒙不会工作。你的算法也很简单incorrect@MartijnPieters,是的,编辑过,但这并不是主要问题。非常感谢您的更正和调试。这是我试图实现的概念。@FilsRato,不用担心。享受mitx
Traceback (most recent call last):
  File "C:\Python34\fib1.py", line 10, in <module>
TypeError: Fastfib() missing 1 required positional argument: 'memo'
def Fastfib(n, memo):
    global numcalls   
    # base case     
    if n < 2:
        return n
    numcalls += n
    print ('fib called with {}'.format(n))
    if n not in memo:
        # FastFib not fastFib
        memo[n] = Fastfib(n-1, memo) + Fastfib(n-2, memo)
    return memo[n] # not memo(n)

res = Fastfib(n,{}) # call with two args

In [5]: print ('fib of {}  is {}.\nRecursive calls {}'.format(n, res, numcalls))
fib of 100  is 354224848179261915075.
Recursive calls 9899