Python Convert 设置为列表,不断收到 TypeError : 'list' object is not callable

标签 python list set

我正在尝试编写一个程序,根据随机数将项目添加到列表中。该计划的一部分可能会滚动额外的项目,但所有重复的项目都应该重新滚动。我的问题是,当我尝试使用我能够找到的方法执行此操作时(将列表与列表集进行比较以测试重复项,然后将该集保存到列表中),但我不断收到 TypeError: 'list' object is不可调用。令人困惑的是,当我使用我设置的简单列表对其进行测试时,它工作正常。

这是我的代码

from random import randint

# Dice roller function
def roll_dice(dice, sides, bonus):
    count = 0
    roll = 0
    for count in range(dice):
        roll += randint(1, sides)
    roll += bonus
    return roll

list = ['1', '2', '3', '4', '5']
print_list = ''
for x in range(0, len(list)-1):
    print_list += ' ' + list[x]
print print_list[1:]

# Minor special armor ability roller
def armor_special_ability():
    reroll = 1
    ability = []
    abil_list = ''
    while reroll > 0:
        result = int(raw_input('Roll:'))#roll_dice(1,100,0)
        if result <= 25:
            ability.append('Glamered')
        elif result <= 32:
            ability.append('Light Fortification')
        elif result <= 52:
            ability.append('Slick')
        elif result <= 72:
            ability.append('Shadow')
        elif result <= 92:
            ability.append('Silent Moves')
        elif result <= 96:
            ability.append('Spell Resistance (13)')
        elif result <= 97:
            ability.append('Improved Slick')
        elif result <= 98:
            ability.append('Improved Shadow')
        elif result <= 99:
            ability.append('Improved Silent Moves')
        elif result <= 100:
            reroll += 2
        reroll -= 1
        if len(ability) > len(set(ability)):
            reroll += (len(ability) - len(set(ability)))
            ability = list(set(ability))
    return ability

print armor_special_ability()

谁能帮我弄清楚为什么我总是收到这个错误?我花了几个小时在网上搜索但没有成功。

最佳答案

问题在行-

list = ['1', '2', '3', '4', '5']

你正在用你的列表覆盖内置的 list 函数,在这个分配之后,如果你尝试调用 list() 它会导致错误,因为它会尝试访问您定义的列表并调用它。

使用不同的名称,永远不要使用 list 作为变量的名称(除非你真的想覆盖 list 内置函数),因为它覆盖内置函数。

关于Python Convert 设置为列表,不断收到 TypeError : 'list' object is not callable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30949890/

相关文章:

algorithm - 拟阵,独特的电路特性

python - 检查用户输入和具有多个值的字典键之间的匹配

javascript - SimpleTemplate 可以忽略模板文件中的变量吗?

python - 使用 Tensorflow 的 tf.io.gfile.exists 检查文件路径是否存在

python - PTVSD Visual Studio 版本不匹配

c - 将由三个结构组成的链表传递给函数

c# - 列出所有位置的覆盖数据

c++ - 更改 STL 多重集中两个相等元素的顺序

Python装饰器

python-3.x - 如果文件用 int 而不是 string 写入,python 中的错误处理