MasterHolbytla MasterHolbytla - 1 month ago 6
Python Question

Call a Python function with arguments based on user input

I would like to call a function from a user input, but include arguments in the parenthesis. For example, if I have a function that takes one argument:

def var(value):
print(value)


I would like to ask the user for a command and arguments, then call the function with the arguments:

Input Command: var("Test")
Test

Answer

Assuming you absolutely trust the input, just use eval

eval(str(input("Command: ")))

"str" is important for some variable types.

Comments