Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用类方法检查输入是否有效_Python - Fatal编程技术网

Python 如何使用类方法检查输入是否有效

Python 如何使用类方法检查输入是否有效,python,Python,在这个程序中,我有一个类来表示一个体积(包含一个数量和一个单位)。当我运行它时,以及在解释器中尝试以下操作时,会出现此错误: >>> A = (10, "mil") >>> print(A) Error: <repr(<__main__.Volume at 0x9ab7cc0>) failed: TypeError: __repr__ returned non-string (type NoneType)> >A=(10,“mil”)

在这个程序中,我有一个类来表示一个体积(包含一个数量和一个单位)。当我运行它时,以及在解释器中尝试以下操作时,会出现此错误:

>>> A = (10, "mil")
>>> print(A)
Error: <repr(<__main__.Volume at 0x9ab7cc0>) failed: TypeError: __repr__ returned non-string (type NoneType)>
>A=(10,“mil”)
>>>印刷品(A)
错误:
我需要先修复这个错误,然后才能检查我的其他方法是否有效,但我似乎找不到如何修复这个错误。这是我的密码:

class Volume(object):

    def __init__ (self, m = 0, u = "ml"):
        self.__magnitude = m
        self.__units = u

    def is_valid (self): #checks if the volume and units are valid
        if type(self.__magnitude) == int and type(self.__units) == str:
            if self.__units == "ml" or self.__units == "oz":
                if self.__magnitude < 0:
                    return True
                else:
                    return False
            else:
                return False
        else:
            return False

    def __repr__ (self):
        if Volume.is_valid(self):
            return str(self.__magnitude) + " " + str(self.__units) #needs fix    

    def __str__ (self):
        if Volume.is_valid(self):
            return round(self.__repr__(),3) #fix    

    def units (self): #returns the units of the volume
        if Volume.is_valid(self):
            return self.__units

    def magnitude (self): #returns the magnitude of the volume
        if Volume.is_valid(self):
            return self.__magnitude

    def metric (self): #transforms the magnitude into metric system if needed
        if Volume.is_valid(self):
            if self.__units == "ml":
                return self.__units
            elif self.__units == "oz":
                mil = self.__units / 0.033814
                return mil

    def customary (self): #transforms the magnitude into customary if needed
        if Volume.is_valid(self):
            if self.__units == "oz":
                return self.__units
            elif self.__units == "ml":
                ozs = self.__units * 0.033814
                return ozs

    def __add__ (self, v2): #adds to magnitude using an int or another volume
        if Volume.is_valid(self):
            if isinstance(v2, Volume):
                if Volume.is_valid(v2): #check if V2 is valid
                    if self.__units == v2.__units:
                        return Volume(self.__magnitude + v2.__magnitude, 
                                      self.__units)
                    else:
                        if v2.__units == 'ml':
                            v3 = v2.customary()
                            return Volume(self.__magnitude + v3.__magnitude, 
                                          self.__units)
                        elif v2.__units == 'oz':
                            v3 = v2.__units
                            return Volume(self.__magnitude + v3.__magnitude, 
                                          self.__units)

            elif isinstance(v2,(int,float)):
                #create new Volume with same units as self
                v3 = Volume(v2, self.__units)
                return Volume(self.__magnitude + v3.__magnitude, self.__units)

    def __radd__ (self, v2): #adds ability to do i.e. 2 + a
        return self.__add__(v2)

    def __sub__ (self, v2): #same as __add__ but this subtracts
        if Volume.is_valid(self):
            if isinstance(v2, Volume):
                if Volume.is_valid(v2):
                    if self.__units == v2.__units:
                        return Volume(self.__magnitude - v2.__magnitude, 
                                      self.__units)
                    else:
                        if v2.__units == 'ml':
                            v3 = v2.customary()
                            return Volume(self.__magnitude - v3.__magnitude, 
                                          self.__units)
                        elif v2.__units == 'oz':
                            v3 = v2.__units
                            return Volume(self.__magnitude - v3.__magnitude, 
                                          self.__units)
            elif isinstance(v2,(int,float)):
                v3 = Volume(v2, self.__units)
                return Volume(self.__magnitude - v3.__magnitude, self.__units)

    def __rsub__ (self, v2):
        return Volume(v2.__magnitude - self.__magnitude, self.__units)

    def __mult__ (self, v2):#multiplies a volume by an int only (not other vol)
        if Volume.is_valid(self):
            return Volume(self.__magnitude * v2.__magnitude, self.__units)

    def __rmult__ (self, v2):
        return self.__mult__(v2)

    def __eq__ (self, v2): #checks if 2 volumes are equal
        if Volume.is_valid(self):
            if isinstance(v2, Volume):
                if Volume.is_valid(v2):
                    if self.__units == v2.__units:
                        return self.__magnitude == v2.__magnitude
                    else:
                        if v2.__units == 'ml':
                            v3 = v2.customary()
                            return self.__magnitude == v3.__magnitude
                        elif v2.__units == 'oz':
                            v3 = v2.__units
                            return self.__magnitude == v3.__magnitude
            elif isinstance(v2,(int,float)):
                v3 = Volume(v2, self.__units)
                return self.__magnitude == v3.__magnitude

    def __noteq__ (self, v2): #checks if 2 volumes are not equal
        if Volume.is_valid(self):
            if isinstance(v2, Volume):
                if Volume.is_valid(v2):
                    if self.__units == v2.__units:
                        return self.__magnitude != v2.__magnitude
                    else:
                        if v2.__units == 'ml':
                            v3 = v2.customary()
                            return self.__magnitude != v3.__magnitude
                        elif v2.__units == 'oz':
                            v3 = v2.__units
                            return self.__magnitude != v3.__magnitude
            elif isinstance(v2,(int,float)):
                v3 = Volume(v2, self.__units)
                return self.__magnitude != v3.__magnitude

    def __lt__ (self, v2): #checks if 1 volume is < than the other volume
        if Volume.is_valid(self):
            if isinstance(v2, Volume):
                if Volume.is_valid(v2):
                    if self.__units == v2.__units:
                        return self.__magnitude < v2.__magnitude
                    else:
                        if v2.__units == 'ml':
                            v3 = v2.customary()
                            return self.__magnitude < v3.__magnitude
                        elif v2.__units == 'oz':
                            v3 = v2.__units
                            return self.__magnitude < v3.__magnitude
            elif isinstance(v2,(int,float)):
                v3 = Volume(v2, self.__units)
                return self.__magnitude <= v3.__magnitude

    def __gt__ (self, v2): #checks if 1 volume is > than the other volume
        if Volume.is_valid(self):
            if isinstance(v2, Volume):
                if Volume.is_valid(v2):
                    if self.__units == v2.__units:
                        return self.__magnitude > v2.__magnitude
                    else:
                        if v2.__units == 'ml':
                            v3 = v2.customary()
                            return self.__magnitude > v3.__magnitude
                        elif v2.__units == 'oz':
                            v3 = v2.__units
                            return self.__magnitude > v3.__magnitude
            elif isinstance(v2,(int,float)):
                v3 = Volume(v2, self.__units)
                return self.__magnitude > v3.__magnitude
    def __lteq__ (self, v2):#checks if 1 volume is <= to other volume
        if Volume.is_valid(self):
            if isinstance(v2, Volume):
                if Volume.is_valid(v2):
                    if self.__units == v2.__units:
                        return self.__magnitude <= v2.__magnitude
                    else:
                        if v2.__units == 'ml':
                            v3 = v2.customary()
                            return self.__magnitude <= v3.__magnitude
                        elif v2.__units == 'oz':
                            v3 = v2.__units
                            return self.__magnitude <= v3.__magnitude
            elif isinstance(v2,(int,float)):
                v3 = Volume(v2, self.__units)
                return self.__magnitude <= v3.__magnitude

    def __gteq__ (self, v2):#checks if 1 volume is >= to other volume
        if Volume.is_valid(self):
            if isinstance(v2, Volume):
                if Volume.is_valid(v2):
                    if self.__units == v2.__units:
                        return self.__magnitude >= v2.__magnitude
                    else:
                        if v2.__units == 'ml':
                            v3 = v2.customary()
                            return self.__magnitude >= v3.__magnitude
                        elif v2.__units == 'oz':
                            v3 = v2.__units
                            return self.__magnitude >= v3.__magnitude
            elif isinstance(v2,(int,float)):
                v3 = Volume(v2, self.__units)
                return self.__magnitude >= v3.__magnitude
类卷(对象):
定义初始化(self,m=0,u=“ml”):
自身大小=m
自身单位=u
def是否有效(自身):#检查音量和单位是否有效
如果类型(自身大小)=int,类型(自身单位)=str:
如果self.\u单位==“ml”或self.\u单位==“oz”:
如果自身大小小于0:
返回真值
其他:
返回错误
其他:
返回错误
其他:
返回错误
定义报告(自我):
如果卷.u有效(自):
返回str(self.\uu数量级)+“”+str(self.\uu单位)#需要修复
定义(自我):
如果卷.u有效(自):
返回回合(自我报告(),3)#修复
def units(self):#返回卷的单位
如果卷.u有效(自):
返回自我单位
def magnity(self):#返回卷的大小
如果卷.u有效(自):
返回自。uuu大小
def metric(self):#如果需要,将震级转换为公制
如果卷.u有效(自):
如果self.\u单位=“ml”:
返回自我单位
elif self.\uu单位=“oz”:
密耳=自身单位/0.033814
回程密耳
def惯常值(self):#如果需要,将大小转换为惯常值
如果卷.u有效(自):
如果自身单位=“盎司”:
返回自我单位
elif self._单位=“ml”:
ozs=自身单位*0.033814
返回分区计划大纲图
def uu add uu(self,v2):#使用一个int或另一个卷来增加大小
如果卷.u有效(自):
如果存在(v2,体积):
如果卷.u有效(v2):#检查v2是否有效
如果自身单位=v2单位:
返回量(自身大小+v2大小,
自身(单位)
其他:
如果v2._单位='ml':
v3=v2.习惯用法()
返回量(自身大小+v3大小,
自身(单位)
elif v2._单位='oz':
v3=v2.单位
返回量(自身大小+v3大小,
自身(单位)
elif isinstance(v2,(int,float)):
#使用与self相同的单位创建新卷
v3=体积(v2,自身单位)
返回量(自身大小+v3.自身大小,自身单位)
def uu radd uu(self,v2):#增加了执行的能力,即2+a
返回自我。添加(v2)
定义(self,v2):#与uuuu add uuuuuuuuuuuuuuuuuuuuuuuuuuuu相同,但这是减去
如果卷.u有效(自):
如果存在(v2,体积):
如果卷有效(v2):
如果自身单位=v2单位:
返回量(自身大小-v2大小),
自身(单位)
其他:
如果v2._单位='ml':
v3=v2.习惯用法()
返回量(自身大小-v3大小,
自身(单位)
elif v2._单位='oz':
v3=v2.单位
返回量(自身大小-v3大小,
自身(单位)
elif isinstance(v2,(int,float)):
v3=体积(v2,自身单位)
返回量(自身大小-v3.自身大小,自身单位)
定义(自我,v2):
返回量(v2.\uuuuu量级-自身.\uuuuu量级,自身.\uuuuu单位)
def uuu mult(self,v2):#仅将一个卷乘以一个整数(而不是其他卷)
如果卷.u有效(自):
返回量(自身大小*v2.自身大小,自身单位)
定义(自我,v2):
返回自我。多个(v2)
def uu eq uu(self,v2):#检查两个卷是否相等
如果卷.u有效(自):
如果存在(v2,体积):
如果卷有效(v2):
如果自身单位=v2单位:
返回自身大小==v2
其他:
如果v2._单位='ml':
v3=v2.习惯用法()
返回自.\uuuu幅值==v3.\uuuu幅值
elif v2._单位='oz':
v3=v2.单位
返回自.\uuuu幅值==v3.\uuuu幅值
elif isinstance(v2,(int,float)):
v3=体积(v2,自身单位)
返回自.\uuuu幅值==v3.\uuuu幅值
def uu noteq uu(self,v2):#检查两个卷是否不相等
如果卷.u有效(自):
如果存在(v2,体积):
如果卷有效(v2):
如果自身单位=v2单位:
返回自我。uu数量级!=v2.uu量级
其他:
如果v2._单位='ml':
v3=v2.习惯用法()
返回自我。uu数量级!=v3.uuu震级
elif v2._单位='oz':
v3=v2.单位
def __repr__ (self):
    if Volume.is_valid(self):
        return str(self.__magnitude) + " " + str(self.__units) #needs fix 
def is_valid (self): #checks if the volume and units are valid
    if type(self.__magnitude) == int and type(self.__units) == str:
        if self.__units == "ml" or self.__units == "oz":
            if self.__magnitude < 0:
                return True
            else:
                return False
        else:
            return False
    else:
        return False
def is_valid (self): #checks if the volume and units are valid
    return (self.__units == "ml" or self.__units == "oz") and \
            self.__magnitude >= 0;
 if Volume.is_valid(self):
      ...
 if self.is_valid():
     ...