+ 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"
22 Antworten
+ 46
def lool():
print ("i like fish")
user=input ()
if user=='lool':
lool()
+ 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.
+ 9
I guess you can switch depending on the input and in the cases do the different functions
+ 8
exec really is a security risk... really a bad idea to use it without proper thought beforehand
+ 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.
+ 5
yes if you use a dictionary like that :
d={"lool":lol} #I am not sure about the syntax
d[input()]()
+ 4
thanks for answering my question it helps allot
+ 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).
+ 3
you can make an if statement so if user inputs "this", it will call "that".
+ 2
Everything is possible if you are creative enough.
+ 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
+ 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')
+ 2
of course it can be called by the user
+ 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.
+ 1
in python 3 you could use
X = input ("are you happy or sad")
if X == "happy":
print ("OK")
your input would be happy.
+ 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")
0
asds yu, your program does work
0
def lool():
print("I like fish")
user_input = input()
temp = globals().copy()
temp.update(locals())
user_input = temp.get(user_input)
user_input()
0
said=input()
if said=='lool':
print("I like fish\n")
- 1
yes definately
def printf(inp):
print(inp)
prinf("lets code")