-1

I need some help, trying to print in the following format:

00: 0   
01: 1   
02: 1   
03: 2   
04: 3   
05: 5   
06: 8   
07: 13  
08: 21  
09: 34  
10: 55  

My codes:

import math
import time

start_time = time.time()
golden_ratio = (1 + math.sqrt(5)) / 2

def  main():
    num = int(input("How many Fibonacci numbers should I print? "))

    for number in range(0,num+1):
        val = (golden_ratio**number - (1 - golden_ratio)**number) / math.sqrt(5)
        print(format(round((val)),'3d'))

main()
sanyassh
  • 8,100
  • 13
  • 36
  • 70
CelestialSky
  • 57
  • 2
  • 14
  • 2
    Possible duplicate of [Display number with leading zeros](https://stackoverflow.com/questions/134934/display-number-with-leading-zeros) – Joshua Nixon May 13 '19 at 21:58
  • 1
    Where is the problem? What error do you get from your code? What is your current result? Always describe it! – Valentino May 13 '19 at 22:01
  • Hey all these are great examples for me. Thank you. I wrote a function that works to use the golden ratio instead of recursion to print Fibonacci numbers. I know there's still the precision issue but the golden ratio is far more efficient and quicker than recursion. Anyway, I'm using python 3.61 and couldn't quite wrap my head around the formatting....Ideally I'd like to be no space between the number and the colon (01: not 01 :) but it's no big deal it's just an exercise for work. Thanks again – CelestialSky May 13 '19 at 22:30

3 Answers3

1
import math
import time

start_time = time.time()
golden_ratio = (1 + math.sqrt(5)) / 2

def  main():
    num = int(input("How many Fibonacci numbers should I print? "))

    for number in range(0,num+1):
        val = (golden_ratio**number - (1 - golden_ratio)**number) / math.sqrt(5)
        print('{}: {:.0f}'.format(number, val))

main()
Mason Caiby
  • 1,846
  • 6
  • 17
1
import math
import time

start_time = time.time()
golden_ratio = (1 + math.sqrt(5)) / 2

def  main():
    num = int(input("How many Fibonacci numbers should I print? "))

    for number in range(0,num+1):
        val = (golden_ratio**number - (1 - golden_ratio)**number) / math.sqrt(5)
        print(format(number, '02d'), ': ', format(round((val)),'3d'))

main()
Anshuman Dikhit
  • 459
  • 2
  • 10
1
def  main():
    stnum = input("How many Fibonacci numbers should I print? ")
    dig = len(stnum)
    num = int(stnum)

    for number in range(0,num+1):
        val = (golden_ratio**number - (1 - golden_ratio)**number) / math.sqrt(5)
        #print(format(round((val)),'3d'))    
        print(f"{number:0{dig}d}: {val:.0f}")

dig is the number of digits of the amount of Fibonacci numbers: if you ask for 100 Fibonacci numbers, dig is 3. I use formatted string literals (available since python 3.6) to format the output.
{number:0{dig}d} prints the integer number with dig leading 0.
{val:.0f} prints a float with no digits after the dot.

If you have an older version of python and formatted string literals are not available, replace the print statement with this:

print("{}: {:.0f}".format(str(number).zfill(dig), val))
Valentino
  • 7,291
  • 6
  • 18
  • 34