[Tutor] Variable help
Ismael Garrido
ismaelgf at adinet.com.uy
Sun Oct 8 15:40:26 CEST 2006
Tom R. escribió:
> I have a number of arrays:
>
> player1 = [data, data, data]
> player2 = [data, data, data]
> player3 = [data, data, data]
> player4 = [data, data, data]
>
> I want to be able to do something like:
>
> count = 2
> if player + count[3] == 5:
> do this
> do that
> count += 1
>
> And that would do this and that to the array 'player2' in slot 3. It adds 1
> to 'count', so next time through it will access the array 'player3'.
> Basically I want to be able to integrate the value of one variable into
> another variables name. It's hard explain, hopefully the code above will
> give you an idea of what I'm trying to achieve.
>
Put the players in a list.
player_list = [player1, player2, player3... ]
So if you want to get to player1 data, you use player_list[0][0...2],
which would be the same as player1[0...2].
If you then want to do something to every player, you could do:
for player in player_list:
do something
> Alternately, how can I use the return of a function as a variables name? eg:
>
> def choose_player(player):
> if player == 1:
> return player1[3
> if player == 2:
> return player2[3]
> if player == 3:
> return player3[3]
> if player == 4:
> return player4[3]
>
> choose_player(2) = 5
>
>
Functions return values. To keep the return value, you simply have to
assign it to a variable:
result = choose_player(3)
And then you can do whatever you want with result. Bear in mind that
result and player3[3] are *not* the same thing, they only have the same
value.
HTH!
Ismael
More information about the Tutor
mailing list