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

25,412

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.

Share:
25,412
Annelli
Author by

Annelli

Updated on September 29, 2021

Comments

  • Annelli
    Annelli over 2 years

    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.

  • SSBakh
    SSBakh over 5 years
    @MatthieuBrucher Crap, I meant the "original" totalCars(). I should clear that up. Thanks.