Search code examples
pythonpython-3.xwhile-loopuser-inputfibonacci

Print the first Fibonacci number which has a certain length


So I'm basically just trying to figure out how I can get this program to print out the first Fibonacci number with the same amount of numbers in length as the number that the user has input. The Fibonacci counting portion of this works fine, I'm just struggling to understand how I can print out a number that corresponds correctly. Having problems specifically understanding why this line of code:

print(str(result[user]))

does not work and what I need to do to make it work.

def fibonacci():
    previous_num, result = 0, 1
    user = input('please enter the length of the fibonacci number you would like to see')
    while True:
        previous_num, result = result, previous_num + result
        print(result)
        print(str(result[user]))


fibonacci()

Solution

  • Let's examine the line you're stuck on:

    print(str(result[user]))
    

    What the code is trying to do here is access the user attribute of the variable result, convert it to a str, and then print it. It's not working, because the variable result is an integer, and doesn't have anything which can be accessed using square brackets.

    Possibly what you want to do is print the length of the result variable. This can be done by converting result to a string using str, and finding its length using len.

    print(len(str(result)))
    

    This, combined with the previous line, print(result), will print out the Fibonacci number, along with the length of the number in digits.

    For example:

    1
    1
    2
    1
    3
    1
    5
    1
    8
    1
    13
    2
    ...
    

    This isn't what we want - we want our function to return the first Fibonacci number that has the amount of digits that our user inputs. To do this, we need to edit our while condition to the following:

    while len(str(result)) < user:
    

    So we check if the length of the result is less than the length our user asked for, and if not, we compute the next Fibonacci number. If it is, we break out of the while loop, and we can return result. Now we can remove the print statements as well.

    The final thing left to do is wrap our input function with int, to convert the number the user inputs to an integer, so we can compare it with the length of result.

    Our final program is then as follows:

    Code:

    def fibonacci():
        previous_num, result = 0, 1
        user = int(input('please enter the length of the fibonacci number you would like to see: '))
        while len(str(result)) < user:
            previous_num, result = result, previous_num + result
        return result
    
    print(fibonacci())
    

    Output:

    please enter the length of the fibonacci number you would like to see: 10
    1134903170
    

    Alternatively: @Sadap in comments

    def fibonacci():
        previous_num, result = 0, 1
        user = int(input('please enter the length of the fibonacci number you would like to see: '))
        min_num = 10**(user-1)
        while result < min_num:
            previous_num, result = result, previous_num + result
        return result
    
    print(fibonacci())