2013-12-16 123 views
-3

當我嘗試運行這個程序(下面的代碼),它返回一個TypeError: 'int' object is not callable'int'對象不可調用?

代碼:

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 

回答

1

的問題是這一行:

技能(力量,技巧,骰子4,骰子12,character_name)

您將技能稱爲函數,但它是在此行之前幾行定義的數字。

3

您不應該有一個變量和一個函數skill

對於strength也是一樣的。

這真的讓翻譯和你自己感到困惑。

給他們其他粉絲名字的未讀。 :)

2

在函數var中,skill是綁定到整數的本地名稱,該整數會影響全局函數skill()。爲其中一個使用不同的名稱。