+ 2
Can you call for functions via user inputs?
5 Answers
+ 9
def f(x):
return 8*x-7
def g(x):
return 5*x+3
cmd = input("command? ")
print(eval(cmd))
"""
But this code should be dangerous if badly used, so it's better to filter/verify user entries, as user can execute any code, as well as incorrect code ^^
Instead do rather something like:
"""
cmd = int(input("choose a command ( 1: f(x), 2: g(x) )? "))
val = int(input("parameter? "))
if cmd == 1:
print(f(val))
elif cmd == 2:
print(g(val))
else:
print("error: unknown command index")
+ 4
yes
+ 1
but how?
+ 1
Certainly