# It is not working to return the list.

Asked 2 months ago, Updated 2 months ago, 3 views

I have a question on Python 3.
I would like to recursion from 1 to 20 for n under the condition that f(0)=1, f(1)=1, f(n)=f(n-1)+f(n-2), n>1.

``````deff(n):
if n == 0 or n == 1:
return1
else:
return f(n-1)+f(n-2)
defmain():
for i in range (11):
print(f(i))
main()
``````

Return value is
1
1
2
3
5
8
13
21
34
55
89

The original code is like this, but I would like to put it on the list when I return, but it doesn't work

``````deff(n):
L = [ ]
an=[ ]
if n == 0 or n == 1:
an=1
L.append(ans)
return L
else:
an=f(n-1)+f(n-2)
L.append(ans)
return L
defmain():
for i in range (11):
print(f(i))
main()
``````

If you write like this, you won't get the same value as above.

How can I put it on the list and return it?

python python3

2022-09-30 11:54

As a separate explanation, I wrote as follows.

``````deff(n):
if n == 0:
return [1]
elifn==1:
return [1,1]
else:
fn1 = f(n-1)
return fn1+[fn1[-1]+fn1[-2]]

defmain():
for i in range (11):
print("f({0})={1}".format(i,f(i)))))

main()
``````

2022-09-30 11:54

Is it necessary to do it with just one function?

``````defg(n):
if n == 0 or n == 1:
return1
else:
return(n-1)+g(n-2)

def(n):
l = [ ]
for i in range(n):
l.append(g(i))
return l

defmain():
print(f(11))

main()
``````

I think it's better to prepare another function like this.

2022-09-30 11:54

I got something different from others.Could it be simpler?

``````deff(n):
if n == 0 or n == 1:
return [1]
else:
return [sum(f(n-1))) + sum(f(n-2))]
def fibonacci():
result = [ ]
for i in range (11):
result.append(f(i)[0])
print(result)
fibonacci()
``````

After that, solve the gradualization formula and use Vine's formula

``````from path import sqrt
def Fibonacci(n):
if n == 0 or n == 1:
return [1]
else:
return Fibonacci(n-1)+[((1+sqrt(5)/2)**n-(1-sqrt(5)/2)**n)/(sqrt(5))]
default():
for i in range (1,12):
print(Fibonacci(i))
result()
``````

There will be some errors, but

`````` [1,1.0]
[1, 1.0, 2.0]
[1, 1.0, 2.0, 3.0000000000000004]
[1, 1.0, 2.0, 3.0000000000000004, 5.000000000000001]
[1, 1.0, 2.0, 3.0000000000000004, 5.000000000000001, 8.000000000000002]
[1, 1.0, 2.0, 3.0000000000000004, 5.000000000000001, 8.000000000000002, 13.000000000000002]
[1, 1.0, 2.0, 3.0000000000000004, 5.000000000000001, 8.000000000000002, 13.000000000000002, 21.000000000000004]
[1, 1.0, 2.0, 3.0000000000000004, 5.000000000000001, 8.000000000000002, 13.000000000000002, 21.000000000000004, 34.00000000000001]
[1, 1.0, 2.0, 3.0000000000000004, 5.000000000000001, 8.000000000000002, 13.000000000000002, 21.000000000000004, 34.00000000000001, 55.000000000000014]
[1, 1.0, 2.0, 3.0000000000000004, 5.000000000000001, 8.000000000000002, 13.000000000000002, 21.000000000000004, 34.00000000000001, 55.000000000000014, 89.00000000000003]
``````

2022-09-30 11:54

## If you have any answers or tips

Popular Tags
python x 4436
android x 1590
java x 1475
javascript x 1385
c x 903
c++ x 831
ruby-on-rails x 681
php x 678
python3 x 651
html x 631