所以我试图实现一个点类,它创建一个点,然后旋转,缩放和平移这个点。这是我目前写的。在class Point:
'''
Create a Point instance from x and y.
'''
def __init__(self, x, y):
self.x = 0
self.y = 0
'''
Rotate counterclockwise, by a radians, about the origin.
'''
def rotate(self, a):
self.x0 = math.cos(this.a) * self.x - math.sin(this.a) * self.y
self.y0 = math.sin(this.a) * self.x + math.cos(this.a) * self.y
'''
Scale point by factor f, about the origin.
Exceptions
Raise Error if f is not of type float.
'''
def scale(self, f):
self.x0 = f * self.x
self.y0 = f * self.y
'''
Translate point by delta_x and delta_y.
Exceptions
Raise Error if delta_x, delta_y are not of type float.
'''
def translate(self, delta_x, delta_y):
self.x0 = self.x + delta_x
self.y0 = self.y + delta_y
'''
Round and convert to int in string form.
'''
def __str__(self):
return int(round(self.x))
此代码中的某些内容正在生成错误。现在我还没有实现错误捕捉,我有一个错误方法在顶部
^{pr2}$
但是如果某个变量不是float类型,我该如何捕捉错误呢?在
下面是我使用的一个if语句:def __init__(self, x, y):
if not isinstance(x, float):
raise Error ("Parameter \"x\" illegal.")
self.x = x
self.y = y
if not isinstance(y, float):
raise Error ("Parameter \"y\" illegal.")
self.x = x
self.y = y
但这给了我一个缩进错误。那么,我如何准确地打印出一条错误消息,确切地说是哪个变量导致了问题呢?在