0
n = int(input('How many terms you want to Generate: '))

i = 1
j = 1
k = 0
print(i,j)
while k < n:
    j += i
    print(j, end=" ")
    i = j-i
    k += 1

The first two numbers are printed on the first line and all other are printed on the second line. I tried to append all in one list but it gives me error.

Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73

1 Answers1

1

You correctly provided the argument end=" " to print inside your loop, but forgot about it for the print before your loop.

n = int(input('How many terms you want to Generate: '))

i = 1
j = 1
k = 0
print(i, j, end=" ")
while k < n:
    j += i
    print(j, end=" ")
    i = j-i
    k += 1

Output:

How many terms you want to Generate: 5
1 1 2 3 5 8 13

Although, note that your solution still does not print the correct number of elements.

Improvements

More elegants solutions exist using generators, which are well-suited to represent infinite sequences.

import itertools

def fib():
    a, b = 1, 1
    while True:
        yield a
        a, b = b, a + b

n = int(input('How many terms you want to Generate: '))
print(*itertools.islice(fib(), n))

Output:

How many terms you want to Generate: 5
1 1 2 3 5
Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73