python - TypeError : 'dict' object is not callable fix in python card game

标签 python dictionary debugging error-handling

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/

相关文章:

python - 从动态创建的基列表创建类

python - Tkinter:避免事件和值更新之间竞争条件的简单方法?

python - 尝试同时求解微分方程

debugging - 在我知道进程 ID 之前将 gdb 附加到进程

java - 无法理解 Spring Boot 代码流程

python - 当我 append 到 Python 中的列表时出现奇怪的行为

python - Django / python : error when get value from dictionary

Python从字典中检索所有项目

python - 如何在Python中的正则表达式中使用字典?

debugging - IDE工具选择-跨平台x86 ASM调试