Noob student
Noob student

Reputation: 35

Python program not working..The main part keep showing error

class point:
    def __init__(self,z,d):
        self.x = z
        self.y = d
    def display(self,z,d):
        self.x = self.z
        self.y = self.d
    def setX(self, z):
        self.x = z
    def setY(self, d):
        self.y = d
    def getX(self):
       return self.x
    def getY(self):
       return self.y
    def show(self):
       print(self.x)
       print(self.y)

p1 = point() //error
print("Point P1:")
p1.show()
print("Updated value:")
p1.display(5, 6)
p1.setX(9)
p1.setY(4)
p1.show()
print("Point P2:")
p2=point()
p2.setX(9)
p2.setY(4)
p2.show()
print("Updated value:")
p2.display(6, 3)
p2.show()

My program keep showing me error that z and d are missing in the object section,,i donno how to correct it i keep trying but more n more errors keeps showing up.

Traceback (most recent call last):
  File "P:\xxxyyy.py", line 29, in <module>
    p1 = point() //error
TypeError: __init__() missing 2 required positional arguments: 'z' and 'd'

Upvotes: 0

Views: 72

Answers (2)

vash_the_stampede
vash_the_stampede

Reputation: 4606

class point: 
    def __init__(self,z,d): 
        self.x = z 
        self.y = d 
    def display(self,z,d): 
        self.x = z #not self.z
        self.y = d #not self.d
    def setX(self, z): 
        self.x = z 
    def setY(self, d): 
        self.y = d 
    def getX(self): 
       return self.x 
    def getY(self): 
       return self.y 
    def show(self): 
       print(self.x) 
       print(self.y) 

First fix this, you are updating with display using two new variables that its taking in, so we just cast those variables not self.z / self.d

p1 = point(1,2)
print("Point P1:")
p1.show()

Create your instance with values that it needs __ini__(self, z, d) needs a z and d value

print("Updated value:")
p1.display(5, 6)
p1.setX(9)
p1.setY(4)
p1.show()

If p1.display is updating the values to 5,6 then why update them again with setX/setY pretty much you are making z=5, d=6 then z=9 d=4

And then you just repeat these small errors for p2

Upvotes: 0

Aquarthur
Aquarthur

Reputation: 619

Don't forget to pass in z and d in the constructor, eg:

p1 = point(1,2)

Also, in the display function, you try to set self.x to self.z and self.y to self.d. self.z and self.d don't exist (self means it should be a class attribute, which it isn't), you should instead use the function's input parameters:

self.x = z
self.y = d

Upvotes: 3

Related Questions