Python Forum
how do I take an input and put it in a function?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how do I take an input and put it in a function?
#1
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)
Reply
#2
How would you take an input from the user and just print it?
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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?
Reply
#5
(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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  difference between forms of input a list to function akbarza 6 1,006 Feb-21-2024, 08:02 PM
Last Post: bterwijn
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,070 Dec-25-2022, 03:00 PM
Last Post: askfriends
  Showing an empty chart, then input data via function kgall89 0 972 Jun-02-2022, 01:53 AM
Last Post: kgall89
  input function question barryjo 12 2,701 Jan-18-2022, 12:11 AM
Last Post: barryjo
  function with 'self' input parameter errors out with and without 'self' called dford 12 3,075 Jan-15-2022, 06:07 PM
Last Post: deanhystad
  Problem with input after function luilong 10 4,070 Dec-04-2021, 12:16 AM
Last Post: luilong
  Exit function from nested function based on user input Turtle 5 2,889 Oct-10-2021, 12:55 AM
Last Post: Turtle
Star I'm getting syntax error while using input function in def. yecktmpmbyrv 1 1,954 Oct-06-2021, 09:39 AM
Last Post: menator01
  Input function cutting off commands at spaces. throwaway34 3 2,189 May-12-2021, 06:40 AM
Last Post: throwaway34
  Defining a function with input abcd 5 3,089 Feb-21-2021, 02:34 AM
Last Post: NullAdmin

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020