1. 程式人生 > >3-28 遇到的問題及解決方法

3-28 遇到的問題及解決方法

blog log 需要 就是 問題 img from import ()

貼源碼:

#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.

3-28 遇到的問題及解決方法