我正在学习python,但不确定如何解决我的问题。我创建了一个类和定义,该类和定义生成带有数字的数组,然后我想创建另一个将每个数字数组相乘的def。我尝试过类似的操作,但出现了错误。
class Hydrology():
def wieloktornosc_tm():
tab = list(i for i in range(600, 43800, 600))
return tab
def wielokrotnosc_nat_deszczu():
wielokrotnosc_q = []
for i in self.wieloktornosc_tm():
q = (15.347 * (1013 / i ** 0.667))
return wielokrotnosc_q.append(q)
print(Hydrologia.wielokrotnosc_tm())
print(Hydrologia.wieloktornosc_nat_deszczu())
答案 0 :(得分:3)
也许您打算这样做?
def wielokrotnosc_nat_deszczu():
wielokrotnosc_q = []
for i in self.wieloktornosc_tm():
q = (15.347 * (1013 / i ** 0.667))
wielokrotnosc_q.append(q)
return wielokrotnosc_q
使用您的代码,它将始终返回None
如注释中所建议,您可以使用如下独立功能来达到相同的效果:
def wielokrotnosc_nat_deszczu():
wielokrotnosc_q = []
for i in range(600, 43800, 600):
q = (15.347 * (1013 / i ** 0.667))
wielokrotnosc_q.append(q)
return wielokrotnosc_q
如果您要将参数传递给该函数,而该函数会因参数而产生不同的结果,那么使用另一个函数来创建tab
就很有意义。
答案 1 :(得分:1)
您的代码段中存在很多错误(名称错误,方法名称缺少self
,而不是循环添加),此代码有效:
class Hydrology():
@staticmethod
def wielokrotnosc_tm():
tab = list(i for i in range(600, 43800, 600))
return tab
@classmethod
def wielokrotnosc_nat_deszczu(cls):
wielokrotnosc_q = []
for i in cls.wielokrotnosc_tm():
q = (15.347 * (1013 / i ** 0.667))
wielokrotnosc_q.append(q)
return wielokrotnosc_q
print(Hydrology.wielokrotnosc_tm())
print(Hydrology.wielokrotnosc_nat_deszczu())
如果您想讨论更改,请下午给我
答案 2 :(得分:0)
请尝试以下变体形式:
class Hydrology():
# You need to have the self keyword in every
# method definition and in case you need
# to add arguments then (self, arg1, arg2, ...etc)
def wieloktornosc_tm(self):
tab = [i for i in range(600, 43800, 600)]
return tab
def wielokrotnosc_nat_deszczu(self):
wielokrotnosc_q = []
for i in self.wieloktornosc_tm():
q = (15.347 * (1013 / i ** 0.667))
return wielokrotnosc_q.append(q)
# You need to create the class object first.
Hydrologia = Hydrology()
# And then access its methods
print Hydrologia.wieloktornosc_tm()
print Hydrologia.wielokrotnosc_nat_deszczu()