You can use the globals
function to obtain a dict of the global namespace so that you call the function named after a string:
globals()[str(sequence[sequenceIndex])]()
Note, however, that the fact that these functions do things so similar that you can iterate through the function names with an index highly suggests that there is no need for these functions to be separate in the first place.
So instead of creating functions named after an index such as board0
, board1
, etc.:
def board0():
print('OOXX')
def board1():
print('XOXO')
you should consider making the index a parameter and making the function adopt different logics and/or data according to this parameter:
boards = [
'OOXX',
'XOXO'
]
def board(x):
print(boards[x])
board(index)
On the other hand, if the logics of these functions do differ greatly, to the point that you would be using a lot of if
statements with distinctly different logics based on the index, it does make sense to make these functions separate, in which case you can do what @Chris_Rands suggests in the comments, to store the function objects in a mapping list or dict, and call the function after retrieving the function object from the mapping according to the index instead:
def tough_board():
print('OOXX')
def tame_board():
print('XOXO')
boards = [tough_board, tame_board]
boards[index]()