Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.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_Python 3.x_Recursion_Fibonacci - Fatal编程技术网

Python 你能告诉我什么';我的代码怎么了?

Python 你能告诉我什么';我的代码怎么了?,python,python-3.x,recursion,fibonacci,Python,Python 3.x,Recursion,Fibonacci,当我调用fib函数时,它不会返回任何内容。你能告诉我我做错了什么吗 def fib(n, List = []): if n > 0: if len(List) == 0 or len(List) == 1: List.append(1) else: List.append(List[len(List) - 2] + List[len(List) - 1]) fib(n - 1, List

当我调用fib函数时,它不会返回任何内容。你能告诉我我做错了什么吗

def fib(n, List = []):
    if n > 0:
        if len(List) == 0 or len(List) == 1:
            List.append(1)
        else:
            List.append(List[len(List) - 2] + List[len(List) - 1])
        fib(n - 1, List)
    else:
        return List

l = fib(5)
print(l)  # >> None
fib(n-1,列表)
更改为
返回fib(n-1,列表)


您的
if
子句不包含
return
语句。@khelwood是对的,您需要将其更改为
return fib(n-1,List)
考虑将问题的标题重命名为更具体的名称。
def fib(n, List = []):
    if n > 0:
        if len(List) == 0 or len(List) == 1:
            List.append(1)
        else:
            List.append(List[len(List) - 2] + List[len(List) - 1])
        return fib(n - 1, List)
    else:
        return List