I am pretty new to programming but I want to define a function like the following with user input:
function = raw_input('What function shall be used? ') #user puts in "(1+1/x)^x"
def f(x): #using this to define
y = (1+1/x)**x
return y
I hope it is clear what I try to do. Could you use input and define a priori that sin is used as math.sin? But how do I define the function afterwards?
Edited to add specification from the comments
Let's say the valid input is python code such as "(1+1/x)**x" like in the question. Could you now use eval
to define a function f(x)
which can be later used?