Python Forum

Full Version: how do I take an input and put it in a function?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
from functool import lru_cache

@lru_cache()
def fibonacci(n):
  if n == 1:
    return 1
  elif n == 2:
    return 1
  elif n > 2:
    return fibonacci(n-1) + (n-2)

for n in range(1,34):
  print(n, ':', fibonacci(n))
but what f I want the programme to take an int input and fibonacci it? (beginner)
How would you take an input from the user and just print it?
Please use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time.

How do you want to input the number? If you want it to be a command line input, you would import sys, and then sys.argv will be a list of the command line arguments. If you want to ask the user for the number, use input:

response = input('What number would you like to fibonacci? ')
Note that in both cases you would need to use int() to convert the string to an integer.
from functools import lru_cache
responce = input('what number')
@lru_cache()
def fibonacci(n):
return fibonacci (responce-1) + (responce - 2)

print('your number has been fibonaccied')
It stil does not use the input in the function?
(Jan-30-2019, 04:44 PM)mitmit293 Wrote: [ -> ]It stil does not use the input in the function?

You have user input as string. You have defined function (return statement should be intended). But you never have run the function. Function can't all by itself run using whatever values available. You should convert user input to int and give it as argument to function.