将其他变量名称中的变量与 exec() 一起使用



我正在为游戏编程一个机器人。我想在变量名称中使用变量。我写了一个简化的代码来检查我的函数,它有效。然后将我的代码放入真正的机器人中,它只是不起作用。 代码有一个显示玩家的类。机器人正在从另一个文件中导入。这正在工作,我已经尝试过了。

我在互联网上搜索了很多,对我来说最好的解决方案是exec((。但如果有更好的我会接受它?

this is the simple code
p1_l=[1,2,3,4,5]
p2_l=[6,7,8,9,10]
p3_l=[11,12,13,14,15]
p4_l=[16,17,18,19,20]
#these are the known lists
a="p1"
#a is deciding if we are going to work with list 1,2,3 or 4
#the value of a is random
b=str(a)+"_l"
#b is now "list_1"
#now i have an function foo()
def foo(a):
return a
#this functions is only an simple example
#now i want to use my b in this function
#i only know one possible way with exec()
exec("r=foo({})".format(b))
print(r)
#but this is not working

那是真正的代码

#----------------------------------------------------------------------------------------------------------------------------------
def get_all_cards():
RANK_ORDER_NUM = '34567890JQKA2'
SUIT_ORDER_NUM = 'DCHS'
all_cards=[]
for i in RANK_ORDER_NUM:
for a in SUIT_ORDER_NUM:
all_cards.append(i+a)
return all_cards
#----------------------------------------------------------------------------------------------------------------------------------
def shuffle_all_cards(cards_list):
import random
random.shuffle(cards_list)
return cards_list
#----------------------------------------------------------------------------------------------------------------------------------
def give_players_cards(shuffled_cards):
p1_cards=shuffled_cards[0:12]
p2_cards=shuffled_cards[13:26]
p3_cards=shuffled_cards[27:39]
p4_cards=shuffled_cards[40:]
return p1_cards,p2_cards,p3_cards,p4_cards
#----------------------------------------------------------------------------------------------------------------------------------
class Player():
def __init__(self,cards,version):
self.version=version
self.cards=cards
def play(self, hand, is_start_of_round, play_to_beat, round_history, player_no, hand_sizes, scores, round_no):
play_modul = __import__("Week_1_Bot_v"+str(self.version))
played_card = play_modul.play(hand, is_start_of_round, play_to_beat, round_history, player_no, hand_sizes, scores, round_no)
return played_card
#----------------------------------------------------------------------------------------------------------------------------------
def game(version_p1, version_p2, version_p3, version_p4):
all_cards=get_all_cards()
#print(all_cards)
shuffeled_cards=shuffle_all_cards(all_cards)
#print(shuffeled_cards)
p1_start_hand,p2_start_hand,p3_start_hand,p4_start_hand=give_players_cards(shuffeled_cards)
import random
player_order=["player1","player1","player3","player4"]
player_order_in_game=random.shuffle(player_order)
p1 = Player(p1_start_hand,2)
p2 = Player(p2_start_hand,2)
p3 = Player(p3_start_hand,2)
p4 = Player(p4_start_hand,2)
p1_hand=p1_start_hand
p2_hand=p2_start_hand
p3_hand=p3_start_hand
p4_hand=p4_start_hand
if ("3D" in p1_hand):
player_beginning="p1"
if ("3D" in p2_hand):
player_beginning="p2"
if ("3D" in p3_hand):
player_beginning="p3"
if ("3D" in p4_hand):
player_beginning="p4"

#print(player_beginning)
#print(p1_hand,p2_hand,p3_hand,p4_hand)
"""
The parameters to this function are:
* `hand`: A list of card strings that are the card(s) in your hand.
* `is_start_of_round`: A Boolean that indicates whether or not the `play` function is being asked to make the first play of a round.
* `play_to_beat`: The current best play of the trick. If no such play exists (you are the first play in the trick), this will be an empty list.
* `round_history`: A list of *trick_history* entries.
A *trick_history* entry is a list of *trick_play* entries.
Each *trick_play* entry is a `(player_no, play)` 2-tuple, where `player_no` is an integer between 0 and 3 (inclusive) indicating which player made the play, and `play` is the play that said player made, which will be a list of card strings.
* `player_no`: An integer between 0 and 3 (inclusive) indicating which player number you are in the game.
* `hand_sizes`: A 4-tuple of integers representing the number of cards each player has in their hand, in player number order. 
* `scores`: A 4-tuple of integers representing the score of each player at the start of this round, in player number order.
* `round_no`: An integer between 0 and 9 (inclusive) indicating which round number is currently being played.
This function should return an empty list (`[]`) to indicate a pass (see "Playing a Round"), or a list of card strings, indicating that you want to play these cards to the table as a valid play.
"""
is_start_of_round=True
play_to_beat=[]
round_history=[]
hand_sizes=[13,13,13,13]
scores=[0,0,0,0]
rounds_no=0
#play1_test = p3.play( p3_hand , is_start_of_round , play_to_beat , round_history , 2 , hand_sizes , scores , rounds_no)
#play1_test = p3.play(['5H', '4D', '4H', '7D', '8D', '8H', '0D', '0C', 'JH', 'QC', 'QS', 'KH', 'AS'], False, ["6H"], [[]], 0, [13, 13, 13, 13], [0, 0, 0, 0], 0)
#print(play1_test) 
#play1_0=0
b=str(player_beginning) + "_hand"
print(b)
def foo(a):
return a
r="foo({})".format(b)
#print(r)





#print("play1_0" + "=" + player_beginning + ".play([" + player_beginning + "_hand" + "]," + str(is_start_of_round) + "," + str(play_to_beat) + "," + "[" + str(round_history) + "]" + "," + str(int(player_beginning[1])-1) + "," + str(hand_sizes) + "," + str(scores) + "," + str(rounds_no) + ")")
#exec("play1_0" + "=" + player_beginning + ".play([" + player_beginning + "_hand" + "]," + str(is_start_of_round) + "," + str(play_to_beat) + "," + str(round_history) + "," + str(int(player_beginning[1])-1) + "," + str(hand_sizes) + "," + str(scores) + "," + str(rounds_no) + ")")
#print(play1_0)

game(2,2,2,2)

问题在于在实际代码中,我的变量"r"没有定义。所以 exec(( 函数没有将"r"定义为p1_hand之一。列表。

我希望这是可以理解的。 谢谢

尼克拉斯

对于测试代码中的示例,我将改用列表字典:

my_dict = {"p1": [elem1, elem2], "p2":[elem3, elem4], ...}

要访问它,您可以执行以下操作:

player = "p1"
player_list = my_dict[player]

最新更新