3-28 遇到的问题及解决方法

贴源码:

#coding= utf-8
import math
class Point:
def move(self,x,y):
self.x = x
self.y = y
def reset(self):
self.move(0,0)
def calculate_distance(self,other_point):
return math.sqrt(
(self.x - other_point.x)**2+
(self.y-other_point.y)**2)
point1 =Point()
point2 =Point()
point1.reset()
point2.move(5,0)
print(point2.calculate_distance(point1))
assert(point2.calculate_distance(point1)==point1.calculate_distance(point2))
point1.move(3,4)
print(point1.calculate_distance(point2))
print(point1.calculate_distance(point1))

错误原因:未给other_point传入参数,同时point2没有move方法。

#from math import sqrt

import math都是常见的两种导入函数的方法,但是得到前辈的经验来说,需要经常使用的是前一种方法,使用的时候也就不需要在方法前加函数名,对应到上面的例子就是在return函数的后面的就是直接sqrt方法而不是math.sqrt.

原文地址:https://www.cnblogs.com/coder-2017/p/8665146.html