Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: '>' not supported between instances of 'method' and 'int'

I hoping someone can help with this.

I have created a class with a function in it that counts the total cars in 4 lists of cars.

On another script I am creating the interface and want to say if the answer to 'totalCars' is bigger than zero then proceed to offer a type of car.

However when I do this I get this error: TypeError: '>' not supported between instances of 'method' and 'int'. Here is the code:

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 

And on the interface script have:

while self.totalCars > 0:

To get around this I tried to use a boolean, like this:

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 

And on the app script I have:

 while self.totalCars is True

But this totally crashed the program and won't run at all.

Any guidance welcome here. Many thanks.

like image 503
Annelli Avatar asked Jan 27 '23 07:01

Annelli


1 Answers

That's because self.totalCars is a method and you need to call it to get it's return value by adding a couple parenthesis at the end, like so:

while self.totalCars() > 0:
     #Insert the rest here

Otherwise, like the message says, you're comparing a method with a number, and that's not gonna work.

No need to add a boolean, but if you insisted on using one, you could do something like:

while self.totalCars():    #Will run if self.totalCars() RETURNS True

Again, this didn't really work in your original code because you forgot the parenthesis.

Hope this helps.

like image 70
SSBakh Avatar answered Mar 06 '23 12:03

SSBakh