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")

16th Jun 2020, 4:39 PM
Cat Sauce
Cat Sauce - avatar
4 Antworten
+ 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)
16th Jun 2020, 5:50 PM
Nova
Nova - avatar
+ 10
You can take the amount as user input and pass it as function argument. inp = int(input("Enter the input: ")) sayHi(inp)
16th Jun 2020, 4:50 PM
Nova
Nova - avatar
+ 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])
16th Jun 2020, 5:14 PM
Cat Sauce
Cat Sauce - avatar
+ 1
Nova thank you :)
16th Jun 2020, 6:06 PM
Cat Sauce
Cat Sauce - avatar