如何寻找bug
class Distance:
def __init__(self,ntxt):
self.x1=eval(ntxt[0])
self.x2=eval(ntxt[1])
self.y1=eval(ntxt[2])
self.y2=eval(ntxt[3])
print(self.y2,self.y1)
def getdistance(self):
distance=pow(pow(self.x2-self.x1, 2) + pow(self.y2-self.y1, 2),0.5)
return distance
ntxt=input('')
nls=ntxt.split(' ')
d=Distance(nls)
distance=d.getdistance()
print("{}".format(distance))
比如这段代码,我封装了一个方法,来求两点之间的最短距离,但是我的输出结果却总为0,这时候就可用Debug来设断点,在各点设置print来得到到各点的输出结果从而定位出问题所在
class Distance:
def __init__(self,ntxt):
self.x1=eval(ntxt[0])
self.x2=eval(ntxt[1])
self.y1=eval(ntxt[2])
self.y2=eval(ntxt[3])
print(self.y2,self.y1,type(self.x2))
def getdistance(self):
print(self.x2,self.x1)
distance=pow(pow(self.x2-self.x1, 2) + pow(self.y2-self.y1, 2),0.5)
print(distance)
return distance
ntxt=input('')
nls=ntxt.split(' ')
d=Distance(nls)
distance=d.getdistance()
print("{}".format(distance))
于是我加上了几个print语句,发现init都成功初始化了下x1.x2,y1,y2,问题出在distance这个表达式上
最终发现在赋值时赋值出错了
改代码时有报错的话先看看时哪里报了错