python - 调用函数时“int”对象不可调用

标签 python int callable dice

我是一名业余程序员,正在开发控制台骰子滚筒,比传统的骰子滚筒复杂得多。到目前为止,我在使用此功能时遇到了问题:

  def multiRoll(dice, amount):
        total = 0
        rolls = []
    for roll in range(amount):
        rolls += roll(dice)
    total = sum(rolls)
    return total, rolls

它的意思是从描述骰子类型(4、6、8 面等)的字典中获取单个条目及其索引和掷骰子的次数。然后它会根据需要掷骰子,然后将每个结果添加到一个列表中,稍后再对其进行求和。

但显然通量出了问题,当它到达这条线时:

        rolls += roll(dice)

我得到“int”对象不可调用

请您告诉我如何克服这个问题?

如果您对 roll() 函数有任何疑问,请参阅:

def roll(dice):
    if dice < 1:
            print "I found a problem when trying to roll a d%d" % (dice)
            return "Failed"
    else:
            return randrange(1, dice)

最佳答案

for roll in range(amount):
        rolls += roll(dice)

roll(dice) 没有使用您的函数,而是使用 for 循环的 roll 变量。您应该将循环的变量更改为:

for number in range(amount):
    rolls += roll(dice) 

或者类似的东西。

关于python - 调用函数时“int”对象不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13406859/

相关文章:

python - python 中的最大迭代项和关键属性

python - ValueError : The truth value of an array with more than one element is ambiguous. 使用 a.any() 或 a.all()

c - 在 C 中从管道写入和读取整数

c++11 - 使用部分可变参数调用函数

按列公式 CPLEX - 在列中添加新的约束集

python - 如何将列表中的每个元素与嵌套列表相乘并获得总和?

python - 使用 F 对象切换查询集中的 boolean 字段

c# - 使用 LINQ 转换为 Int

python - 如何从python numpy中的矩阵中获取 float

python - 字节数组上的 iter(callable, sentinel) 形式