如何将无类型输入列表?

时间:2018-09-23 23:42:48

标签: python python-3.x python-2.7

此函数返回Nonetype而不是列表,我不知道为什么。我如何使这件事返回列表? 这是代码:

def fib_less_than(N):
    fibonacci_numbers = [1,1]
    fibonacci_n = fibonacci_numbers[-1] + fibonacci_numbers[-2]
    while fibonacci_n < N in range(2,N+5):
        fibonacci_n = fibonacci_numbers[-1] + fibonacci_numbers[-2]
        fibonacci_numbers.append(fibonacci_n)
    print(fibonacci_numbers[0:len(fibonacci_numbers)-1])

fib_less_than(10)

2 个答案:

答案 0 :(得分:1)

那是因为您什么也没返回。因此,您必须退还Fib。列表,然后在功能之外进行打印:

def fib_less_than(N):
    fibonacci_numbers = [1,1]
    fibonacci_n = fibonacci_numbers[-1] + fibonacci_numbers[-2]
    while fibonacci_n < N in range(2,N+5):
        fibonacci_n = fibonacci_numbers[-1] + fibonacci_numbers[-2]
        fibonacci_numbers.append(fibonacci_n)
    return (fibonacci_numbers[0:len(fibonacci_numbers)-1])

print (fib_less_than(10))

答案 1 :(得分:0)

在Python中,print返回一个None(它仅在控制台中显示内容)。您需要在功能结束时return