Krithika Krishnan
Krithika Krishnan

Reputation: 11

printing fibonacci from input number

So I tried printing Fibonacci sequence using an input number. I'm not sure how to input a number into my code.

def fibonacci(n):
    a,b=0,1
    while(a<n):
        print(a,end=' ')
        a,b=b,a+b
    print()

fibonacci(fibo_entry=input("enter number"))

I get this Error:

TypeError                                                           Traceback (most recent call last)
    <ipython-input-113-d552685b93df> in <module>()
       7         a,b=b,a+b
       8     print()
    ----> 9 fibonacci(fibo_entry=input("enter number"))

    TypeError: fibonacci() got an unexpected keyword argument 'fibo_entry'

Upvotes: 0

Views: 1648

Answers (3)

jimmy
jimmy

Reputation: 117

you need to use typecasting for your 'input' function, do like this:

num=int(input("enter number: "))
fibonacci(num)

OR do like this:

fibonacci(int(input("enter number: ")))

Upvotes: 0

Albert Rothman
Albert Rothman

Reputation: 1010

In this line, the python interpreter thinks you are trying to specify an argument for fibonacci.

fibonacci(fibo_entry=input("enter number"))

Easiest fix is to separate it out. You also must cast as an int, because input returns a string:

fibo_entry=int(input("enter number"))
fibonacci(fibo_entry)

Upvotes: 1

MSeifert
MSeifert

Reputation: 152647

The TypeError is because your function doesn't take an fibo_entry-argument. You could call it like this:

fibonacci(input("enter number"))

However this will give you another error because the input always returns a string on python3, so you need to cast it to a number:

import ast
fibonacci(ast.literal_eval(input("enter number")))

or explicitly:

fibonacci(int(input("enter number")))

However I would recommend catching the input as seperate variable and just pass that variable to the function:

fibo_entry = int(input("enter number"))
fibonacci(fibo_entry)

Upvotes: 4

Related Questions