-1
我使用python 3.6並且想創建一個Interval類。我試圖做一個可以添加一個整數和一個intervall的add方法。到目前爲止我設法生成用於 工作I =間隔(2,3) C = I + 1#C = [3,4]類和方法python
然而相反dosent工作
I =間隔的碼(2,3) C = 1 + I#獲取錯誤消息類型錯誤:不支持的操作數類型(一個或多個)爲+: 'INT' 和 '間隔'
我爲我的類和方法的代碼是作爲folow中
類間隔:
def __init__(self,start=None,end=None):
if end==None:
end=start
if start==None:
start=end
if start>end:
raise TypeError('left value must be smaller than right value')
self.start=start
self.end=end
高清添加(個體經營,其他):
if isinstance(other,int):
s2,e2=other,other
s1,e1=self.start,self.end
elif isinstance(self,int):
s1,e1=self,self
s2,e2=other.start,other.end
else:
s2,e2=other.start,other.end
s1,e1=self.start,self.end
return Interval(s1+s2,e1+e2)
如果我試圖改變我的elif在Add方法,如果沒有什麼作品。有誰知道如何解決這個問題?
謝謝!
謝謝!它現在通過radd方法工作! –
不客氣。 –