gpt4 book ai didi

python - TypeError: 'tuple' 对象在尝试调用方法时不可调用

转载 作者:太空宇宙 更新时间:2023-11-04 08:58:33 25 4
gpt4 key购买 nike

这是我目前所做的事情:

# -*- coding: cp1252 -*-
import time

class Item():
def __init__(self, name, description, base_value):
self.name = name
self.description = description
self.ingredients = ingredients
self.base_value = value
def __str__(self):
return format(self.name, self.description, self.ingredients, self.base_value)


class Metal(Item):
def __init__(self, name, description, ingredients, base_value):
self.smelt_time = smelt_time
self.smelted = smelted
def __str__(self):
return format(self.name, self.description, self.ingredients, self.base_value, self.smelt_time, self.smelted)

class Bronze_Ingot(Metal):
def __init__(self):
self.name = "Bronze Ingot",
self.description = "A refined ingot of bronze."
#self.ingredients = Tin_Ore(1)+Copper_Ore(1) <--- I will get these lines working later...
self.base_value = 33
self.smelt_time = 15
self.smelted = ()

class Fuel(Item):
def __init__(self, name, description, ingredients, base_value):
self.fuel_time = fuel_time
def __str__(self):
return format(self.name, self.description, self.ingredients, self.base_value, self.fuel_time)

class Cloth(Fuel):
def __init__(self):
self.name = "Cloth",
self.description = "A piece of cotton cloth."
#self.ingredients = 2 Cotton <--- I will get these lines working later...
self.base_value = 2
self.fuel_time = 5

但是我在使用这个函数时遇到了很大的麻烦......

    def smelted(Fuel, Metal):
if (Fuel.fuel_time - Metal.smelt_time) > 0:
time.sleep(1)
print "Smelting", Metal.name, "..."
time.sleep(Metal.smelt_time)
print "Time to forge!"

问题或多或少是让它发挥作用。我和我的 friend 尝试了运行此功能时我们能想到的一切,但都无济于事。这是我们最近的尝试:

from Smelting_Progress import *

x = Cloth()
y = Bronze_Ingot()

y.smelted(x,y)

尝试运行后,我收到了这个错误:

Traceback (most recent call last):
File "C:\Users\WCS-HSSTUDENT\Desktop\Files\Project SAOffline\Coding\New Aincrad World\Items\NAI_Smelted.pyw", line 6, in <module>
Metal.smelted(Fuel, Metal)
TypeError: 'tuple' object is not callable

最佳答案

你有一个实例属性smelted;你在 Metal.__init__() 中设置它:

self.smelted = smelted

您的 Bronze_Ingot 子类将其设置为一个空元组:

self.smelted = ()            

您不能让方法和元组使用相同的名称。重命名一个或另一个。

如果您打算将您的smelted() 代码用作函数,则在顶层定义它(与您的类相同的缩进),并将其命名为一个函数,而不是一个方法:

smelted(x, y)

(注意,前面没有y.)。

关于python - TypeError: 'tuple' 对象在尝试调用方法时不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27927103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com