'int'对象不可调用?

时间:2013-12-16 13:11:31

标签: python python-3.x

当我尝试运行此程序(下面的代码)时,它返回TypeError: 'int' object is not callable

代码:

import random
import math

def var():
    strength = 10
    skill = 10
    dice4 = 0
    dice12 = 0

    character_name = str(input("Please enter your characters name: "))
    skill(strength, skill, dice4, dice12, character_name)

def skill(strength, skill, dice4, dice12, character_name):

    print(character_name + "'s attributes are being generated! ... ")

    dice4, dice12 = random.randrange(1,4), random.randrange(1,12) 

    dice_score = dice12/dice4
    dice_score = math.floor(dice_score)
    skill = skill + dicescore

    strength(strength, skill, dice4, dice12, character_name)

def strength(strength, skill, dice4, dice12, character_name):
    dice4, dice12 = random.randrange(1,4), random.randrange(1,12) 

    dice_score = dice12/dice4
    dice_score = math.floor(dice_score)
    strength = strength + dicescore
    file(strength, skill, dice4, dice12, character_name)

def file(strength, skill, dice4, dice12, character_name):
    file = open("N:\Controlled Assessment - Ryan Harper\Task Two\attributes.txt", w)
    file.writelines(character_name + " - Strength = " + str(strength) + ", Skill = " + str(skill))

var()

错误:

Traceback (most recent call last):
  File "N:\Controlled Assessment - Ryan Harper\Task Two\task 2 v2.py", line 37, in <module>
    var()
  File "N:\Controlled Assessment - Ryan Harper\Task Two\task 2 v2.py", line 11, in var
    skill(strength, skill, dice4, dice12, character_name)
TypeError: 'int' object is not callable

3 个答案:

答案 0 :(得分:3)

你不应该有一个名为skill的变量和一个函数。

strength也是如此。

这真的让翻译和你自己感到困惑。

给他们其他狂热的名字。 :)

答案 1 :(得分:2)

在函数var中,skill是绑定到整数的本地名称,它隐藏全局函数skill()。其中一个使用不同的名称。

答案 2 :(得分:1)

问题在于这一行:

技能(力量,技能,dice4,dice12,character_name)

您将技能称为函数,但它是一个在此行之前定义几行的数字。