Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to fix TypeError: class() takes no arguments problem?

I am trying to create a class name RangePrime and it’s instance attributes should print out the range of them and finally appear an issue. How to fix them?

class RangePrime:
def range_list(self,a,b):
    self.a = a
    self.b = b
    x = []
    if b < a:
        x.extend(range(b, a))
        x.append(a)
    else:
        x.extend(range(a, b))
        x.append(b)
    return x

after i ran this -> range_prime = RangePrime(1, 5) and it start to appear
TypeError: RangePrime() takes no arguments

I should get the following result:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>>> range_prime = RangePrime(1, 5)
>>> range_prime.range_list
[1, 2, 3, 4, 5]

>Solution :

It looks you have mixed using functions without a class definition and at the same time misdefining the functions within the class by lacking an __init__(). I have modified your code a bit to account for the class and its functions, so the intention of your code remains the same. Kindly try:

class RangePrime():
    def __init__(self, a,b):
        self.a = a
        self.b = b
        
    def range_list(self):
        a = self.a
        b = self.b
        x = []
        if b < a:
            x.extend(range(b, a))
            x.append(a)
        else:
            x.extend(range(a, b))
            x.append(b)
        return x

Which when running:

range_prime = RangePrime(1, 5)
range_prime.range_list()

Returns:

[1,2,3,4,5]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading