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:
>>> 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]