I am making a program in Python 3.5 which will run different functions when you enter different inputs:
commandList = ["test1", "test2", "test3"]
def test1():
print("TEST 1 FUNCTION")
def test2():
print("TEST 2 FUNCTION")
def test3():
print("TEST 3 FUNCTION")
while True:
userRaw = input(">:")
user = userRaw.lower()
for x in range(len(commandList)):
if user == commandList[x]:
# Needs to run function which has the same name as the string 'user'
# E.g. if user = 'test1' then function test1() is run.
What would I need to enter after the if
statement (where the comments are)?
I have tried doing something like this, but it has not worked:
commandList = ["test1", "test2", "test3"]
def function(test1):
print("TEST 1 FUNCTION")
def function(test2):
print("TEST 2 FUNCTION")
def function(test3):
print("TEST 3 FUNCTION")
while True:
userRaw = input(">:")
user = userRaw.lower()
for x in range(len(commandList)):
if user == commandList[x]:
function(user)
I am trying to avoid a lots of if
statements as I aim to make my code easily scalable (quickly add new functions).