Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.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,以下是与我的问题相关的代码:(编辑了完整的代码,对于由此造成的任何混乱,我深表歉意) class House: 定义初始(自身、位置、价格、户型、平方英尺): self.location=str(位置) self.price=浮动(价格) self.housetype=str(housetype) self.sqft=int(sqft) 如果self.price100: self.sqft=newSqft 其他: 打印(“输入有效值”) def税(自付,无年): 如果self.getHouset

以下是与我的问题相关的代码:(编辑了完整的代码,对于由此造成的任何混乱,我深表歉意)

class House:
定义初始(自身、位置、价格、户型、平方英尺):
self.location=str(位置)
self.price=浮动(价格)
self.housetype=str(housetype)
self.sqft=int(sqft)
如果self.price<0或self.sqft<100:
打印(“输入有效值”)
def getLocation(自):
返回自我定位
def getPrice(自):
返回自己的价格
def getHousetype(自):
返回自类型
def getSqft(自身):
返回self.sqft
def设置位置(自身、新位置):
self.location=newLocation
def setPrice(自我、新价格):
如果newPrice>0:
self.price=newPrice
其他:
打印(“输入有效值”)
def setHousetype(自身、新房屋类型):
self.housetype=newHousetype
def设置QFT(自身、新闻QFT):
如果newSqft>100:
self.sqft=newSqft
其他:
打印(“输入有效值”)
def税(自付,无年):
如果self.getHousetype==“公寓”:
返回自身价格*(0.025*无年)
elif self.getHousetype==“平房”:
返回自身价格*(0.035*无年)
elif self.getHousetype=='Condominium':
返回自身价格*(0.045*无年)
其他:
打印(“无效数据”)
定义(自身、其他):
如果self.getHousetype()=其他.getHousetype():
返回“true”
其他:
返回“false”
定义(自我、无年、其他):
如果自税(无年)<其他税(无年):
返回“true”
我根据我的格式创建了两个house变量,但是当我运行
house1.\uu lt\uuuu5,house2)
时,它给出了错误:


'
self.getHousetype
本身就是一个方法对象,因此
self.getHousetype=='partment'
始终为false。您需要调用该方法,即,
self.getHousetype()==“公寓”
。由于此时所有条件都为false,因此没有任何return语句发生,函数以返回默认值
none
结束

但正如cricket_007所指出的,python不需要getter方法,所以只需执行
self.housetype==“公寓”


self.housetype

什么是
self.getHousetype
?它与您的任何条件都不匹配,因此该函数不返回任何内容(
None
)。在else语句中返回某个内容将是一个好的开始。
getHousetype
函数或类的属性?注意:python不需要getter方法当我们不给函数返回任何值时,函数返回
None
。在上述情况下,函数
tax
中有return语句,如果
条件不满足,则函数return
None
。因此,将
return 0
语句添加到
tax
函数的末尾。Vivek建议的一个变体是:添加引发异常的else语句。如果您再次运行它并得到一个异常而不是
NoneType
错误,您就知道问题是因为所有条件都不匹配。如果您没有,您可以更新您的问题并向我们保证:-)我真的很惊讶我之前没有注意到这一点。。我认为在getter方法末尾添加()之后,一切都可以正常工作。我参加的当前课程需要使用getter方法,所以我想我应该更加关注下次不要犯这样的错误。对不起,我不理解引起的骚动!
class House:
def __init__(self, location, price, housetype, sqft):
    self.location = str(location)
    self.price = float(price)
    self.housetype = str(housetype)
    self.sqft = int(sqft)

    if self.price < 0 or self.sqft < 100:
        print("Input a valid value.")

def getLocation(self):
    return self.location

def getPrice(self):
    return self.price

def getHousetype(self):
    return self.housetype

def getSqft(self):
    return self.sqft

def setLocation(self, newLocation):
    self.location = newLocation

def setPrice(self, newPrice):
    if newPrice > 0:
        self.price = newPrice
    else:
        print("Input a valid value.")

def setHousetype(self, newHousetype):
    self.housetype = newHousetype

def setSqft(self, newSqft):
    if newSqft > 100:
        self.sqft = newSqft
    else:
        print("Input a valid value.")

def tax(self, no_years):
    if self.getHousetype == 'Apartment':
        return self.price * (0.025 * no_years)
    elif self.getHousetype == 'Bungalow':
        return self.price * (0.035 * no_years)
    elif self.getHousetype == 'Condominium':
        return self.price * (0.045 * no_years)
    else:
        print("Invalid data.")

def __eq__ (self, other):
    if self.getHousetype() == other.getHousetype():
        return 'true'
    else:
        return 'false'

def __lt__ (self, no_years, other):
    if self.tax(no_years) < other.tax(no_years):
        return 'true'