Python 大家好,您建议在空for循环之后放什么(我不知道放什么)?
这是使用距离公式计算两个坐标之间距离的代码。Python 大家好,您建议在空for循环之后放什么(我不知道放什么)?,python,class,for-loop,Python,Class,For Loop,这是使用距离公式计算两个坐标之间距离的代码。 import math class Line(): def __init__(self,coor1,coor2): self.coor1=coor1 self.coor2=coor2 def fun(self): self.coor1=[x_one,y_one] self.coor2=[x_two,y_two] for e in [self.co
import math
class Line():
def __init__(self,coor1,coor2):
self.coor1=coor1
self.coor2=coor2
def fun(self):
self.coor1=[x_one,y_one]
self.coor2=[x_two,y_two]
for e in [self.coor1,self.coor2]:
**what to put in??!!**
u=((self.coor1[0]-self.coor2[0])**2+(self.coor1[1]-self.coor2[1])**2)
p=math.sqrt(u)
print(f'the answer is{p}')
fun()
方法必须读取self.coor1
,self.coor2
,并且不能再次设置它们。(它们已在
\uuuu init\uuuu()方法中设置。)
输出: 距离是5.0
通过
?我不明白你为什么一开始就需要这个循环。对self.coor1
和self.coor2
的分配似乎没有必要(即使x_one
等已定义);当您实例化行
时,它们可能已正确初始化。谢谢!你帮了我这么多much@lilvanrossum,不客气。如果你想帮我查看我的个人资料并查看我的最后一个问题,我需要关于for循环的c语言帮助(我是初学者)
import math
class Line():
def __init__(self, coor1, coor2):
self.coor1 = coor1
self.coor2 = coor2
def fun(self):
x_1, y_1 = self.coor1
x_2, y_2 = self.coor2
u = (x_2 - x_1) ** 2 + (y_2 - y_1) ** 2
return math.sqrt(u)
### Using this Line class
point1 = (1, 4)
point2 = (5, 7)
line = Line(point1, point2)
p = line.fun()
print(f'The distance is {p}.')