2

I am trying to print the fibonacci series using lists in python. This is my code:

T = int(raw_input())
N = [int(raw_input()) for i in range(T)]
N[:] = [x-2 for x in N]
L1 = [1, 1]
L2 = []
for j in N:
    for k in range(j):
        L1.append(L1[-1]+L1[-2])
    L2.append(L1)
print L2

Here, T denotes the number of test cases. N denotes the number of elements in the fibonacci series.I need to print the required output of the fibonacci series for each test case.

Input: 
2
4
5

Output for the code above:

[[1, 1, 2, 3, 5, 8, 13], [1, 1, 2, 3, 5, 8, 13]]

However the output that I want to print is:

[1, 1, 2, 3], [1, 1, 2, 3, 5]]

Please let me know how I should proceed on this

2
  • 3
    Out of curiousity - what's the next task you're doing? We've answered the 3 and 5s one, now it appears the Fibonacci one... I'll take a guess primes? Commented May 31, 2015 at 0:55
  • Yes, I am trying to do the projectEuler problems. I am much new to python .I know that the solutions are already out on net. But, I want to go with my own methodology. Despite me giving the best shot, I am getting stuck. Please let me know if I am not supposed to ask the questions like these here. Commented May 31, 2015 at 1:06

1 Answer 1

2

Your problem is that you don't reset L1. Try:

T = int(raw_input())
N = [int(raw_input()) for i in range(T)]
N[:] = [x-2 for x in N]
L2 = []
for j in N:
  L1 = [1, 1]
  for k in range(j):
    L1.append(L1[-1]+L1[-2])
  L2.append(L1)
print L2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.