1

Here is my source code:

def fibonacci_numbers():
  how_many_numbers = int(input("\nHow many Fibbonacci numbers you want print: "))
  i = 0
  res = [0, 1]
  while i < how_many_numbers:
      res.append(sum(res[-2:]))
      print("{}. {}".format(i + 1, res[i]))
      i += 1


fibonacci_numbers()

The output now is:

How many Fibbonacci numbers you want print: 30
1. 0
2. 1
3. 1
4. 2
5. 3
6. 5
7. 8
8. 13
9. 21
10. 34
11. 55
12. 89
13. 144
...
30. 514229

But I need something like that:

How many Fibbonacci numbers you want print: 30
1.       0
2.       1
3.       1
4.       2
5.       3
6.       5
7.       8 
8.      13
9.      21
10.     34
11.     55
12.     89
13.    144
...
30. 514229

and so on. It's depending for a numbers I choose. If i take 50 numbers so I need more spaces, but everything need to be placed to the right side.

How I can achieve this?

I tried with {:>width} and with .rjust method, but I can't get it.

Is there simple solution for that?

Or maybe I need make another loop and print there?

Final Solution:

Thanks for answers. I pick up some of them and i made this:

def fibonacci_numbers():

    how_many_numbers = int(input("\nHow many Fibbonacci numbers you want print: "))
    i = 0
    res = [0, 1]
    while i < how_many_numbers:
        res.append(sum(res[-2:]))
        i += 1

    size_of_i = len(str(i))
    i = 0
    for num in res[:-1]:
        size = len(str(res[-2]))
        print("{:<{width_of_i}} {:>{width}}".format(str(i)+":", num, width_of_i=size_of_i + 1, width=size))
        i += 1



fibonacci_numbers()

6 Answers 6

1

Try to format both values:

import math

def fibonacci_numbers():
  how_many_numbers = int(input("\nHow many Fibbonacci numbers you want print: "))
  i = 0
  res = [0, 1]
  for i in range(how_many_numbers):
      res.append(sum(res[-2:]))
  digits = int(math.log10(res[i]))+1
  ind_digits = int(math.log10(how_many_numbers-1))+2
  for i in range(how_many_numbers):
      print("{:<{ind_digits}} {:>{digits}}".format("{}.".format(i + 1), res[i], ind_digits=ind_digits, digits=digits))


fibonacci_numbers()

UPD: support fo variable length added.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that helped!
@patruk91, please consider upvoting my answer :)
I don't have enough reputation now to cast vote, but I will do that when I get it. I promise! :). I marked it as the most helpful one! Thanks again.
1

Change your print statement to this

print("{}. {:10}".format(i + 1, res[i])) # for left justify
print("{}. {:>10}".format(i + 1, res[i])) # for right justify

If you need more information on how to do it, refer to this link

1 Comment

Is not what I mean, because if Fibonacci number will be more than 10 char the stairs will start.
1

You can use the format-string to justify to the right. Try this for your print:

print("{}. {:>20}".format(i + 1, res[i]))

Comments

0

Try with:

def fibonacci_numbers():
how_many_numbers = int(input("\nHow many Fibbonacci numbers you want print: "))
i = 0
res = [0, 1]
while i < how_many_numbers:
    res.append(sum(res[-2:]))
    print("{}. \t\t{}".format(i + 1, res[i]))
    i += 1


fibonacci_numbers()

1 Comment

I don't think this will yield what the OP is requesting.
0

As you do not know how many digits the last number has, you have to store the results in a list and then add spaces while printing.

For example get the length of last number as a string

size = len(str(numbers[-1])))
padding = ""
for i in range(size):
    padding += " " 

and then loop through the array adding the needed spaces:

for num in numbers:
    print(padding[len(str(num)):] + str(num))

1 Comment

Thanks, that helped!
0

You need to specify formatting width to align it to the right correctly.

Code:

def fibonacci_numbers(format_width):
  how_many_numbers = int(input("\nHow many Fibbonacci numbers you want print: "))
  i = 0
  res = [0, 1]
  while i < how_many_numbers:
      res.append(sum(res[-2:]))
      # Here we specify alignment to right with certain width 
      # which is >= number of digits of the largest integer in your sequence.:
      print("{}. {:>{width}}".format(i + 1, res[i], width=format_width)) 
      i += 1


fibonacci_numbers(30)

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.