0

I have written a program where I need to input a length and it returns the digits in the Fibonacci number sequence.

Write a program that given an input n, outputs the first n Fibonacci numbers. The format of the output is that at most 4 numbers can be displayed in a row.

My problem is, when i run the program the output shows [0, 1, 1, 2, 3, 5, 8, 13, 21, 34].

I need the solution to show 4 numbers per row ie

0 1 1 2

3 5 8 13

21 34

Please find attached my code.

def number(n):
    if n ==1:
        print(0)
    else:
        li = [0, 1]
        for i in range(2, n): # start the loop from second index as 'li' is assigned for 0th and 1st index
            li.append(li[-1] + li[-2])
            i += 1
        print(li)

n = int(input('Enter a positive number:'))
number(n)

Can you please explain how I can achieve the desired result?

Yakob
  • 159
  • 1
  • 7
  • 1
    Do you really want the brackets and commas in there? – Tim Roberts Apr 04 '22 at 22:26
  • 1
    does this help: https://stackoverflow.com/a/312464/2681662 – MSH Apr 04 '22 at 22:27
  • 1
    And, to be useful, your function should probably just return the whole list instead of printing it, and let the caller worry about how to present it to the user. – Tim Roberts Apr 04 '22 at 22:27
  • @TimRoberts, no i don't want the brackets and commas in the output, but unsure how to remove them from the output. I'm fairly new to python. – Yakob Apr 04 '22 at 22:36
  • 1
    The KEY is, don't print the whole list. That lets Python decide how to print it. You need to have control over that, by printing individual list elements. @a6623's answer does that, although in a somewhat tricky way. I suspect your teacher expected you to just print individual elements, but keep a counter, and add a newline every 4th one. – Tim Roberts Apr 04 '22 at 23:37
  • @TimRoberts, yep the teacher wants us to produce the results with a maximum of 4 values per row, I will look over the other answer and see if I can implement that thanks – Yakob Apr 04 '22 at 23:56

1 Answers1

1

The trouble is this line of code:

print(li)

which only prints out a big list.

Instead, split the list into groups of 4:

li = [li[i:i+4] for i in range(0, len(li), 4)]

Now you can print each element in this list, with a newline so that they are on different lines:

print(*li, sep="\n")

This "unpacks" the list into its elements (which are themselves lists of at most length 4), then prints them out, separating each element with a newline.

Putting it all together:

def number(n):
    if n ==1:
        print(0)
    else:
        li = [0, 1]
        for i in range(2, n): # start the loop from second index as 'li' is assigned for 0th and 1st index
            li.append(li[-1] + li[-2])
            i += 1
        li = [li[i:i+4] for i in range(0, len(li), 4)]
        print(*li, sep="\n")

>>> number(10)
[0, 1, 1, 2]
[3, 5, 8, 13]
[21, 34]
AndW
  • 726
  • 6
  • 31
  • would you be able to rewrite the code to incorporate my displayed code as when I try to do it with your lines of code I break my console and cant get an answer to appear. thanks – Yakob Apr 05 '22 at 00:11
  • 1
    @Yakob Please see the updated answer. if it answered your question feel free to mark the checkmark, or reply if you have further questions! – AndW Apr 05 '22 at 19:31
  • Hi @a6623, that works great. thanks for your help I appreciate you taking the time to help me and answer this for me :) – Yakob Apr 05 '22 at 22:11