2017-10-23 19 views
0

개체의 변수 값을 수정하는 함수가있는 클래스를 만들어야합니다. 수준의 개 :개체 값을 변경하는 방법은 무엇입니까?

class dog: 

    def __init__(self,x,y,z): 
     self.x=x 
     self.y=y 
     self.z=z 
    def __str__(self): 

     return print("mi nombre es "+self.x) 
    def bark(self,otr): 
     if self.z*self.y>otr.z*otr.y: 
      print("gano: "+self.x) 

      int(otr.x)=int(otr.x)-2 

     if self.z*self.y<otr.z*otr.y: 
      print("gano: "+otr.x) 

      int(self.x)=int(self.x)-2 

하지만 난 그것을 실행하려고 할 때 오류 "호출 캔트 할당 기능"나에게 제공도 나는이 난 그냥 수정 한 올바른 접근 방식

+0

변경'INT (otr.x) = INT (otr.x) -2'에 의해'otr.x = INT (otr.x) -2' . 'int (otr.x)'는 당신이'otr.x'가 정수라고 확신 할 필요는 없습니다. – dcg

+0

아니면 더 간단합니다 :'otr.x - = 2'와'self.x - = 2'. – ekhumoro

답변

0

있는지 알고 싶어 여기 네 개 수업. 아래 코드에서 내 수정 및 코멘트를 참조하십시오 -

class dog: 
def __init__(self,x,y,z): 
    self.x=x 
    self.y=y 
    self.z=z 

def __str__(self): 
    return print("mi nombre es "+self.x)  

def bark(self,otr): 
    if self.z*self.y>otr.z*otr.y: 
     print("gano: "+self.x) 
     otr.x = int(otr.x)-2 
    if self.z*self.y<otr.z*otr.y: 
     print("gano: "+otr.x) 
     self.x = int(self.x)-2 #what were you doing by int(self.x) on the left hand site which I removed. Don't do anything randomly. you need to understand what you are doing. For example - int(val) here val will be converted into integer from any value like string, double etc. Hence left hand side it does not mean anything. 
관련 문제