0
Can i call a function from user input in python
can i call a function from user input like: input: sayHi(3) output: hi hi hi def sayHi(amount): for i in range(amount): print("hi")
4 Answers
+ 9
Sorry, I did a mistake in reading your question. So you want to use input as a function. You mean something like this:
##############
def myFunc(a):
eval(a) # though many people suggest to avoid eval
def sayHi(fn):
for i in range(fn):
print('Hi',end=" ")
func= input('Enter your first function name:')
param = input('Enter your function name with paramter:')
fun = globals()[func]
fun(param)
##############
Input format:
myFunc
sayHi(3)
+ 10
You can take the amount as user input and pass it as function argument.
inp = int(input("Enter the input: "))
sayHi(inp)
+ 2
Nova i know you can call a raw function with this code snippen, but this wont lets you enter a parameter, do you have any idea how i can enter a parameter like this?
def testfunc(fn):
fn()
funcname = input('enter function name: ')
if callable(globals()[funcname]):
testfunc(globals()[funcname])
+ 1
Nova thank you :)