TypeError: 'dict' object is not callable
当前正在调试项目的纸牌游戏,无法解决该错误。任何帮助,将不胜感激!
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
最佳答案
当对象实际上是player_classes
时,您尝试将Callable
用作dict
,因此您应使用dict[key]
表示法访问其元素。
您需要将list comprehension更改为
self.players = [player_classes[player_type] for player_type, _ in player_info]
请注意,您正在创建类型为第一个元素,名称为第二个元素的元组。编辑:忘记将变量名更改为其他名称(
type
是Python函数)。
关于python - TypeError : 'dict' object is not callable fix in python card game,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65828304/