I'm quite new to Python (and programming past pascal) so I'm looking for a relatively simple-to-understand way to do this. All help is much appreciated.

I'm looking for the program to take a variable as if it were just normal code, I'll explain via example.
I want the code to access this:

moves.movesCustom.move1

where move1 can be from 1 to 4.

I have a function to calculate what move to use, and that returns either, move1, move2, move3 or move4.

I'm looking for some way to make something like this work:

moves.movesCustom.chosenMove

Where chosenMove is a string containing either move1, move2, move3 or move4.

I know I could make 4 if statements (I read that Python can't do case statements?)

if chosenMove == move1:
moves.movesCustom.move1;
if chosenMove == move2:
...

but that doesn't seem very efficient. Is there a better way to do this?
Thanks in advance.

What is moves? Module, package or object. How about movesCustom?

If you are trying just to use Pascal struct, I think that simple way to replace that it by tuple.

Like this:

import random
def move1():
    print('move1')

def move2():
    print('move2')

def move3():
    print('move3')

def move4():
    print('move4')


moves = (move1,move2, move3, move4)

for i in range(10):
    chosen_move=random.choice(moves)
    chosen_move()
    
""" Output:
move2
move3
move4
move4
move4
move3
move2
move4
move4
move4
"""

moves is a folder containing a few .py files (moveCustom being one of them) and __init__ which contains the import instructions. Not that confident with importing at the moment but it seems to work.
It's just for importing all the different moves and their settings, so I had "import moves" at the start of the code.
movesCustom is the .py file containing some modules, it contains the chosenMove() module, which returns either move1,move2,move3 or move4.

At the moment I'm not looking for it to be random, it's player input.
move1 is a dictionary, with damage, accuracy and other values.

print moves.movesCustom.chosenMove();

Given that the function chosenMove returns either move1, move2, move3 or move4, I'm aiming for it to display something like:

{'damage':10, 'range':......}

as if it had read that line of code as

print moves.movesCustom.move1;

Just for testing purposes at the moment.

As I gather it I put together this toy package based on info I understood from you. Attached as zip file as it has dictionary. I took out your camelCase, to more pythonic way. Semicolon, by the way, is possible to use in Python to put two commands in same line, but must consider, when it is useful. Putting semicolon at end of line is not useful. If you want print to continue after space in same line as before, comma , is used, not ;

Thanks for all your help.
That seems to work just how I want it to, and it's not very different to my broken version..
I think my problem is that I return a variable in the function, whereas you return a command.
Where you have the random, I have this:

def playerMoveInput():
    valid = False;
    while valid == False:
        moveInput = raw_input('Choose your move, 1, 2, 3 or 4.\n');
        if moveInput == '1' or '2' or '3' or '4':
            valid = True;
    moveInput = 'move'+moveInput;
    return moveInput;

Is there any way to make the output of this behave like yours?:

def chosen_move():
    return random.choice(moves)

And display the value of 'move1' instead of displaying a string with the value 'move1'?

You said earlier that Python cannot do case statements.
It can somewhat do that, in a different fashion, like this:

if a==1:
    pass #do something
elif a==2:
    pass #do some other thing
else:
    pass

which is like

switch(a)
{
    case 1:
        //do something
        break;
    case 2:
        //do some other thing
        break;
    default:
        break;
}

Back the the subject,
there are two ways for you to do that.
One way is using exec(),
which takes any string and executes it.
Another is by doing:

from moves.movesCustom import move1, move2, move3, move4

#...

moveInput = "move" + moveInput
return globals()[moveInput]

I have to say that I don't like either alternative, becuase there is a much simpler, cleaner one. There is a built-in function named getattr with the property that getattr(x, 'a') yields the value of x.a . Similarly, setattr(x, 'a', y) has the same effect as x.a = y .

I have to say that I don't like either alternative, becuase there is a much simpler, cleaner one. There is a built-in function named getattr with the property that getattr(x, 'a') yields the value of x.a . Similarly, setattr(x, 'a', y) has the same effect as x.a = y .

That's right, I forgot about that one.

omg i love getattr and setattr

Ahh right, close enough to case statements, I'm comparing it to Pascal, probably not a great idea, cheers for the tip!
Getattr worked, thanks guys! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.