[英]Missing 1 required positional argument - Python
TypeError: sequence() missing 1 required positional argument: 'n', sequence()
Apparently when using sequence(n-1) + sequence(n-2)
the n is not using the value from the function, what can I do to fix it? 显然,当使用
sequence(n-1) + sequence(n-2)
,n没有使用该函数中的值,我该怎么做才能解决该问题?
memo = {0:0,1:1}
def sequence(type, n):
if type == "fibonacci":
if not n in memo:
memo[n] = sequence(n-1) + sequence(n-2)
else:
return memo[n]
Try this: 尝试这个:
sequence(type, n-1) + sequence(type, n-2)
The error is explicit, the function sequence
is expecting two parameters, but you're passing only one. 错误很明显,函数
sequence
需要两个参数,但是您只传递了一个。 As a side note, you should remove the else
, and make sure that return memo[n]
is executed at the end - because your function must always return a value, otherwise the recursion won't work. 附带说明,您应该删除
else
,并确保最后执行return memo[n]
-因为您的函数必须始终返回一个值,否则递归将不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.