I am creating a game with Python and the Pygame module and I was wondering if there was a way to call a function based on the value of a variable.
I have a variable called 'levels' and to switch the levels, for now I am using an if statement finding the value of the variable and executing the corresponding function. It looks something like this :
if levels == 1:
level1()
if levels == 2:
level2()
etc
Is there a way to execute the function based on the value of the variable?
I know you can combine it to form a string so maybe call a function based on the value of the string?
Something like :
str = 'level'+str(levels)
# call the value of str as a function