Home > Net >  How to I call this method?
How to I call this method?

Time:11-11

I have this piece of code and I feel so dumb for not knowing how to run it. Please help.

class Solution(object):
    def countOdds(self, low: int, high: int):
        if low % 2 == 0 and high % 2 == 0:
            return (high-low)//2
        else:
            return (high-low)//2   1

I tried running Solution.countOdds(3, 11) but the error showed me that I haven't called self, and I don't know how to make it work.

CodePudding user response:

You're trying to access a method of the class but haven't created an instance. Try:

class Solution(object):...

instance = Solution()
print(instance.countOdds(3,11))

CodePudding user response:

You need to create an object linking to the class and then call that. So instead of doing Solution.countOdds(3, 11), you need to do

MyObject = Solution()
MyObject.countOdds(3, 11)

Hope this helps :)

CodePudding user response:

Your function has 3 arguments, but you are calling it using two arguments, i.e Solution.countOdds(3, 11).

  • Related