Home > Enterprise >  '>=' not supported between instances of 'method' and 'int'
'>=' not supported between instances of 'method' and 'int'

Time:01-17

Here is the method causing the error:

class Kilpailu():
    def __init__(self, nimi, pituuskm, osallistujat):
        self.nimi = nimi
        self.pituuskm = pituuskm
        self.osallistujat = osallistujat

    def kilpailu_ohi(self):
        for i in self.osallistujat:
            if (i.getKuljettuMatka>=self.pituuskm):
                return True
            else:
                return False

edit: here is also where getKuljettuMatka is defined

class Auto:
    def __init__(self, rekisteritunnus, huippunopeus):
        self.rekisteritunnus = rekisteritunnus
        self.huippunopeus=huippunopeus
        self.nopeus=0
        self.KuljettuMatka=0
    def getKuljettuMatka(self):
        return int(self.KuljettuMatka)

I am trying to call the method that just returns a boolean value and print whether the value is true or false.

class main():
    autot = []
    for i in range(10):
        auto = Auto("ABC-"   str(i   1), random.randint(100, 200))
        autot.append(auto)

    k = Kilpailu("Suuri romuralli", 8000, autot)
    tunnit = 0
    print(k.kilpailu_ohi()) #Should return true/false, instead throws an error

and here is the actual console output for the error

Traceback (most recent call last):
  File "/root/PycharmProjects/ohjelmisto1/Harjoitustehtävät/Assosisaatio/autoKilpailu.py", line 51, in <module>
    class main():
  File "/root/PycharmProjects/ohjelmisto1/Harjoitustehtävät/Assosisaatio/autoKilpailu.py", line 59, in main
    print(k.kilpailu_ohi())
  File "/root/PycharmProjects/ohjelmisto1/Harjoitustehtävät/Assosisaatio/autoKilpailu.py", line 45, in kilpailu_ohi
    if (i.getKuljettuMatka>=self.pituuskm):
TypeError: '>=' not supported between instances of 'method' and 'int'

I have tried changing stuff in the method like variable names in case i was accidentally overwriting something but it didnt work either

CodePudding user response:

def getKuljettuMatka(self): defines .getKuljettuMatka on instances of Auto as a method (that happens to return an integer). You can call the method with i.getKuljettuMatka() (note the parentheses), but what you're doing is comparing the method itself to self.pituuskm, which is an integer, and that doesn't work.

Instead:

    def kilpailu_ohi(self):
        for i in self.osallistujat:
            if (i.getKuljettuMatka() >= self.pituuskm):
                return True
            else:
                return False

By the way, I think the code is a bit roundabout in other ways, but this is you main issue right now.

  • Related