【发布时间】:2018-10-20 13:02:40
【问题描述】:
我希望有人可以帮助解决这个问题。
我创建了一个类,其中包含一个函数,用于计算 4 个汽车列表中的汽车总数。
在另一个脚本上,我正在创建界面并想说明“totalCars”的答案是否大于零,然后继续提供一种汽车。
但是,当我这样做时,我得到了这个错误:TypeError: '>' not supported between instances of 'method' and 'int'。代码如下:
def totalCars(self):
p = len(self.getPetrolCars())
e = len(self.getElectricCars())
d = len(self.getDieselCars())
h = len(self.getHybridCars())
totalCars = int(p) + int(e) + int(d) + int(h)
return totalCars
并且在界面脚本上有:
while self.totalCars > 0:
为了解决这个问题,我尝试使用布尔值,如下所示:
def totalCars(self):
p = len(self.getPetrolCars())
e = len(self.getElectricCars())
d = len(self.getDieselCars())
h = len(self.getHybridCars())
totalCars = int(p) + int(e) + int(d) + int(h)
if totalCars > 0:
return True
在我的应用脚本上:
while self.totalCars is True
但这完全使程序崩溃并且根本无法运行。
欢迎在这里提供任何指导。 非常感谢。
【问题讨论】:
-
请格式化您的代码。
-
@SimonH 我编辑了帖子,因为缩进在那里,在破坏格式的代码块之前只缺少空行。 Annelli:当您提出/编辑您的问题时,请使用预览功能,以确保它看起来像预期的那样。
-
它是
self.totalCars(),带括号。您没有调用该方法。 -
len() 返回一个整数,不需要 int 强制转换。
-
谢谢安德拉斯,我回去编辑,看到有人已经有了。第一次发帖。非常感谢!
标签: python comparison-operators