【发布时间】:2017-11-05 19:52:26
【问题描述】:
所以我试图创建一个对象,该对象基本上有一个构造函数,它采用两个坐标,xcoord 和ycoord。我进一步创建了移动坐标的方法,我必须检查该点是否有效(有效性标准是坐标是否超出指定范围,它应该返回False,否则返回True)。
问题:
我的课程只返回初始点的有效性,而不是移动点。
我需要什么来更正我的代码?
代码:
class Point:
MaxScreenSize=10
def __init__(self,x,y):
self.xcoord=x
self.ycoord=y
if 0>self.xcoord or self.xcoord>Point.MaxScreenSize or 0>self.ycoord or self.ycoord>Point.MaxScreenSize:
Point.isValidPt=False
else:
Point.isValidPt=True
def translateX(self,shiftX):
self.xcoord=self.xcoord+shiftX
def translateY(self,shiftY):
self.ycoord=self.ycoord+shiftY
测试代码:
我尝试了我的代码,它只返回 isValidFunction 变量作为我的初始点(给我 True 而不是 False 用于以下代码)
p=Point(9,2)
p.translateX(20)
p.translateY(10)
p.isValidPt
【问题讨论】:
标签: python python-3.x class boolean