+ 10

Can a function be called by user input?

I want to create an interactive python app allowing the user to create commands and play a song or something along those lines so I was wondering if functions can be called whith the user input so far it hasent worked i used this coad def lool(): print ("i like fish") input () so if the user typed lool it would print "i like fish"

30th Jan 2017, 1:31 PM
noobi
23 Answers
+ 46
def lool(): print ("i like fish") user=input () if user=='lool': lool()
30th Jan 2017, 10:14 PM
ramzi
ramzi - avatar
+ 14
Here you go: def lool(): print("i like fish") x = input() x = x + "()" exec(x) The exec() function is useful when you want to treat strings like executable code. Not sure if it's the most elegant solution but it works.
30th Jan 2017, 2:17 PM
Doc
Doc - avatar
+ 9
I guess you can switch depending on the input and in the cases do the different functions
2nd Feb 2017, 1:14 PM
C.E.
C.E. - avatar
+ 8
exec really is a security risk... really a bad idea to use it without proper thought beforehand
30th Jan 2017, 9:33 PM
Amaras A
Amaras A - avatar
+ 6
What you are looking for is introspection. Look for function getattr. in python docs. Introspection is about self information included in classes. Here is an example: class Functions: @classmethod def lol(cls): print("I like fish") @classmethod def dosomething(cls,something): print(something) def process(functionName, args): try: f = getattr(Functions, functionName) except AttributeError: print ("Function '{0}' do not exist".format(functionName)) else: try: if (args == ""): f() else: f(args) except TypeError: print ("Wrong argument '{0}' to function {1}".format(args, functionName)) while True: print() fname = input("Print function name: " ) fargs = input("Print function args: " ) print() if (fname == ''): print ("Exit") break; else: process(fname, fargs) To add more options you only need to add class methods to Functions class.
14th Feb 2017, 8:59 AM
Francisco Gonzalez
Francisco Gonzalez - avatar
+ 5
yes if you use a dictionary like that : d={"lool":lol} #I am not sure about the syntax d[input()]()
30th Jan 2017, 2:11 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 4
thanks for answering my question it helps allot
30th Jan 2017, 10:00 PM
noobi
+ 4
*cough* Python is an _interpreter_ :) ===================== def lool(): print('i like fish') eval(input('Type lool!') + "()") ===================== Output: Type lool! (you type function name) i like fish Security issue's similar to exec() : trusting user input with NO filter...except it's for one statement (exec's for a batch).
18th Feb 2017, 10:08 PM
Kirk Schafer
Kirk Schafer - avatar
+ 3
you can make an if statement so if user inputs "this", it will call "that".
30th Jan 2017, 8:55 PM
MYSTIC_ burger
MYSTIC_ burger - avatar
+ 2
Everything is possible if you are creative enough.
7th Feb 2017, 6:32 PM
Shahar Levy
Shahar Levy - avatar
+ 2
def lool() : print("i like fish") inp = input ("Please enter a callable keyword :") if inp == "lool" : lool() else : print ("oohhh , you do not like fish") #sorry for bad english #add and subscribe šŸ˜:3
15th Feb 2017, 3:12 PM
lux arcadia
lux arcadia - avatar
+ 2
def a(): print ("a" *10) def b(): print ("b" *10) def c(): print ("c" *10) x = input('command:') if x in ['a', 'b', 'c']: exec(x+'()') else: print ('not defined')
22nd Feb 2017, 7:07 PM
Hamid Ariannejad
Hamid Ariannejad - avatar
+ 2
of course it can be called by the user
27th Feb 2017, 5:44 PM
sameer siddiqui
sameer siddiqui - avatar
+ 1
a=input('If you type \'lool\', I will tell you something.') if a=='lool': print('I like fish') else: print('Good bye!') #I don't know this is right or not.
7th Feb 2017, 6:30 PM
asds yu
asds yu - avatar
+ 1
in python 3 you could use X = input ("are you happy or sad") if X == "happy": print ("OK") your input would be happy.
20th Feb 2017, 1:55 PM
Nicholas Begg
Nicholas Begg - avatar
+ 1
can't be a function like that, but u can do this: X = input("are you happy or sad") if X == "happy": print ("that is good")
21st Feb 2017, 12:33 PM
Nicholas Begg
Nicholas Begg - avatar
0
asds yu, your program does work
7th Feb 2017, 6:33 PM
ramzi
ramzi - avatar
0
def lool(): print("I like fish") user_input = input() temp = globals().copy() temp.update(locals()) user_input = temp.get(user_input) user_input()
12th Feb 2017, 10:25 PM
Bob Liou
Bob Liou - avatar
0
said=input() if said=='lool': print("I like fish\n")
19th Feb 2017, 10:26 PM
Hello World!!!!
Hello World!!!! - avatar
- 1
yes definately def printf(inp): print(inp) prinf("lets code")
11th Feb 2017, 6:32 PM
Madhurima Chakraborty
Madhurima Chakraborty - avatar