In Python I want to use user input as a function call. In particular, I have the following code I'm testing out:
def forward(forwardValue):
print("in forward, with value " + forwardValue)
return
command = 'forward' # this would come in as user input
value = '255'
command(value)
What I would like to see is a printout that says: in forward, with value 255
What I do see is an error that:
'str' object is not callable
That error message makes sense, and I can see why what I tested doesn't work, but is there any way to do what I want to do? In other words, can user input be used as a function call?
What I do currently instead is use an "if" statement to check if the user input is the 'forward' statement, and if so to call the function 'forward'. But I would like to be more Pythonesque if possible.
I have a list of about 30 different commands the user can use, each of which commands has a defined function to handle it. My series of 30 "if" and "elif" statements seems clumsy.