用 类 形成一个多边形
Forming a polygon with classes
所以,我的问题是:我正在尝试创建一个程序,该程序将创建一个至少具有 3 个点(由坐标 x 和 y 组成)或角度的多边形。我希望,如果提交的点或角度少于 3 个,程序 returns 会出现错误,指出点数不足。我需要用 类 创建它。
到目前为止我已经创建了这个:`
class Polygon:
number_points = 0
number_angles = 0
def __init__(self, coordinate_x, coordinate_y, angles):
s = []
self.coordinate_x = coordinate_x
self.coordinate_y = coordinate_y
self.angles = angles
self.s = s.append([coordinate_x, coordinate_y])
Polygon.number_points = Polygon.number_points + 1
Nkotnik.number_angles = Polygon.number_angles + 1
# Here i would like the program to check if there are enough points
# and angles to form a polygon and to check if all coordinates are
# numbers. If this requirement is not met, the program prints an
# error message.
def creation(self):
if not isinstance(coordinate_x, (int,float)):
#raise Exception("That is not a number")
if Polygon.number_points <= 3:
`
我的想法是将坐标存储在一个列表中,然后当用户输入足够多的点时,就可以形成一个多边形。
我不是母语人士,所以如果我需要进一步澄清问题,请随时提问 :) 感谢您提供任何可能的答案 :)
我在这里看到一个错误:
Polygon.number_points = Polygon.number_points + 1
Nkotnik.number_angles = Polygon.number_angles + 1
Nkotnik
应该是 Polygon
。另外,为了缩短它,你可以做 Polygon.number_points += 1
和 number_angles
.
一样
那么现在,创建程序:
def creation(self):
这是糟糕的设计。该函数应将点数和角度数作为参数。所以,这样做:
def creation(self, points, angles):
但是creation
基本上就是initialization
,所以你应该把它融入你的__init__
。
还有,你的__init__
很奇怪。 number_points
和 number_angles
应该在 __init__
中定义,而不是对象主体,因为这些变量对于不同的 Polygon
对象是不同的。所以修改后,你的代码是这样的:
class Polygon:
def __init__(self, coord_list, angles):
if len(coord_list) // 2 < 3:
raise Exception("Side count must be 3 or more.")
s = []
self.number_points = 0
self.number_angles = 0
self.coordinates_x = coord_list[::2]
self.coordinates_y = coord_list[1::2]
self.angles = angles
self.s = s.append([coordinate_x, coordinate_y])
self.number_points += len(coord_list // 2)
self.number_angles += len(angles)
num_sides = int(input('Number of sides: ')) #raw_input if you're using Python 2
points = []
angles = []
for i in range(num_sides):
points.append(int(input('X value of point: ')))
points.append(int(input('Y value of point: ')))
for i in range(num_sides):
angles.append(int(input('Angle value: ')))
polygon_object = Polygon(points, angles)
大功告成!
您可以在 class 中创建时进行检查,就像这样,您还需要更多的东西来定义一个点
import collections
PointCartesian = collections.namedtuple("PointCartesian","coordinate_x coordinate_y")
PointPolar = collections.namedtuple("PointPolar","magnitude angle")
#this is a easy way to make a class for points, that I recommend have
#a class too
class Polygon(object):
def __init__(self,*argv,**kargv):
points = list()
for elem in argv:
if isinstance(elem,(PointCartesian,PointPolar ) ):
points.append(elem)
else:
raise ValueError("Element "+str(elem)+" of wrong type")
if len(points) <3:
raise ValueError("Insufficient data")
self.points = points
并且在其他地方,您有向用户询问数据的例程,您可以检查每个输入或将其留给 class。
像这样调用它
Polygon(PointCartesian(1,2),PointCartesian(4,7),PointPolar(5,28.2))
Polygon(*list_of_points)
所以,我的问题是:我正在尝试创建一个程序,该程序将创建一个至少具有 3 个点(由坐标 x 和 y 组成)或角度的多边形。我希望,如果提交的点或角度少于 3 个,程序 returns 会出现错误,指出点数不足。我需要用 类 创建它。
到目前为止我已经创建了这个:`
class Polygon:
number_points = 0
number_angles = 0
def __init__(self, coordinate_x, coordinate_y, angles):
s = []
self.coordinate_x = coordinate_x
self.coordinate_y = coordinate_y
self.angles = angles
self.s = s.append([coordinate_x, coordinate_y])
Polygon.number_points = Polygon.number_points + 1
Nkotnik.number_angles = Polygon.number_angles + 1
# Here i would like the program to check if there are enough points
# and angles to form a polygon and to check if all coordinates are
# numbers. If this requirement is not met, the program prints an
# error message.
def creation(self):
if not isinstance(coordinate_x, (int,float)):
#raise Exception("That is not a number")
if Polygon.number_points <= 3:
`
我的想法是将坐标存储在一个列表中,然后当用户输入足够多的点时,就可以形成一个多边形。
我不是母语人士,所以如果我需要进一步澄清问题,请随时提问 :) 感谢您提供任何可能的答案 :)
我在这里看到一个错误:
Polygon.number_points = Polygon.number_points + 1
Nkotnik.number_angles = Polygon.number_angles + 1
Nkotnik
应该是 Polygon
。另外,为了缩短它,你可以做 Polygon.number_points += 1
和 number_angles
.
那么现在,创建程序:
def creation(self):
这是糟糕的设计。该函数应将点数和角度数作为参数。所以,这样做:
def creation(self, points, angles):
但是creation
基本上就是initialization
,所以你应该把它融入你的__init__
。
还有,你的__init__
很奇怪。 number_points
和 number_angles
应该在 __init__
中定义,而不是对象主体,因为这些变量对于不同的 Polygon
对象是不同的。所以修改后,你的代码是这样的:
class Polygon:
def __init__(self, coord_list, angles):
if len(coord_list) // 2 < 3:
raise Exception("Side count must be 3 or more.")
s = []
self.number_points = 0
self.number_angles = 0
self.coordinates_x = coord_list[::2]
self.coordinates_y = coord_list[1::2]
self.angles = angles
self.s = s.append([coordinate_x, coordinate_y])
self.number_points += len(coord_list // 2)
self.number_angles += len(angles)
num_sides = int(input('Number of sides: ')) #raw_input if you're using Python 2
points = []
angles = []
for i in range(num_sides):
points.append(int(input('X value of point: ')))
points.append(int(input('Y value of point: ')))
for i in range(num_sides):
angles.append(int(input('Angle value: ')))
polygon_object = Polygon(points, angles)
大功告成!
您可以在 class 中创建时进行检查,就像这样,您还需要更多的东西来定义一个点
import collections
PointCartesian = collections.namedtuple("PointCartesian","coordinate_x coordinate_y")
PointPolar = collections.namedtuple("PointPolar","magnitude angle")
#this is a easy way to make a class for points, that I recommend have
#a class too
class Polygon(object):
def __init__(self,*argv,**kargv):
points = list()
for elem in argv:
if isinstance(elem,(PointCartesian,PointPolar ) ):
points.append(elem)
else:
raise ValueError("Element "+str(elem)+" of wrong type")
if len(points) <3:
raise ValueError("Insufficient data")
self.points = points
并且在其他地方,您有向用户询问数据的例程,您可以检查每个输入或将其留给 class。
像这样调用它
Polygon(PointCartesian(1,2),PointCartesian(4,7),PointPolar(5,28.2))
Polygon(*list_of_points)