我有一個類:範圍類蟒蛇
class Difference:
def __init__(self,a1):
self.a1=a1
def computeDifference(self):
d0=max([max(self.a1)-i for i in self.a1])
return d0
maximumDifference=d0
現在,當我嘗試訪問像波紋管得到波紋錯誤類:
_ = input().strip()
a = [int(e) for e in input().strip().split(' ')]
d = Difference(a)
d.computeDifference()
print(d.maximumDifference)
錯誤:
Traceback (most recent call last): File "q.py", line 2, in class Difference: File "q.py", line 8, in Difference maximumDifference=d0 NameError: name 'd0' is not defined
什麼出錯?
你從來沒有告訴pytho什麼'd0'是,當你嘗試設置'maximumDifference'到'd0'蟒蛇拋出你的錯誤的內部屬性。在類中定義'd0''Difference'來避免錯誤 –