+ 2

Can you call for functions via user inputs?

6th Mar 2017, 8:33 AM
Mask
Mask - avatar
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")
7th Mar 2017, 7:39 AM
visph
visph - avatar
+ 4
yes
6th Mar 2017, 8:37 AM
Meharban Singh
Meharban Singh - avatar
+ 1
but how?
6th Mar 2017, 9:10 AM
Mask
Mask - avatar
+ 1
Certainly
6th Mar 2017, 9:28 AM
Zhao Xiaoqi
Zhao Xiaoqi - avatar