TypeError: 'dict'对象不可调用
目前正在为一个项目调试一个纸牌游戏,不知道如何修复这个错误。任何帮助将不胜感激!
player_info = UI.get_player_information(MAX_PLAYERS)
self.players = [player_classes(name) for name, typ in player_info]
参考代码:
player_classes = {
'human': Player,
'simple': SmartAI,
'smart': SimpleAI,
}
def get_player_information(max_players):
"""get required information to set up a round"""
# create players list
player_info = []
# how many human players?
print("nHow many human players [1-4]:")
no_of_players = get_int_input(1, max_players)
# for each player, get name
for i in range(no_of_players):
print(f"Please enter the name of player {i+1}:")
player_info.append(('human', get_string_input()))
ai_names = ['Angela', 'Bart', 'Charly', 'Dorothy']
# how many AI players? ensure there are at least 2 players
min_val = 1 if (len(player_info) == 0) else 0
max_val = max_players - no_of_players
print(f"nHow many ai players [{min_val:d}-{max_val:d}]:")
no_of_players = get_int_input(min_val, max_val)
# randomly assign a simple or smart AI for each computer strategy
for name in ai_names[:no_of_players]:
if [True, False]:
player_info.append(('simple', name))
else:
player_info.append(('smart', f"Smart {name}"))
return player_info
当对象实际上是dict
时,您试图使用player_classes
作为Callable
,因此您应该使用符号dict[key]
来访问其元素。
你需要把你的列表推导式改成
self.players = [player_classes[player_type] for player_type, _ in player_info]
请注意,创建元组时,第一个元素是类型,第二个元素是名称。
编辑:忘记将变量名更改为其他内容(type
是Python函数)。