需要 return 来匹配打印显示的内容
Need return to match what print is showing me
我需要 return 给出最后一行打印的内容。
在底部,我用值调用了 class。另外,欢迎指点以改进代码。
class Building:
def __init__(self, south, west, width_WE, width_NS, height=10):
# making variables non-local
self.south=int(south)
self.west=int(west)
self.width_WE=int(width_WE)
self.width_NS=int(width_NS)
self.height=height
self.d={}
self.d['north-east']=(south+width_NS,west+width_WE)
self.d['south-east']=(south,west+width_WE)
self.d['south-west']=(south,west)
self.d['north-west']=(south+width_NS,west)
self.wwe=width_WE
self.wns=width_NS
self.height=10
def corner(self): # gives co-ordinates of the corners
print(self.d)
def area (self): # gives area
print(self.wwe*self.wns)
return(self.wwe*self.wns)
def volume(self): #gives volume
print(self.wwe*self.wns*self.height)
def __repr__(self): # I dont know what to call it but answer should be''Building(10, 10, 1, 2, 2)''
print ("Building(%s,%s,%s,%s,%s)"%(self.south, self.west, self.width_WE, self.width_NS,"10"))
#return ("Building(%s,%s,%s,%s,%s)"%(self.south, self.west, self.width_WE, self.width_NS,"10"))
abc = Building(10, 10, 1, 2, 2)
abc.corner()
abc.area()
abc.volume()
改用__str__
:
def __str__(self):
return "Building({0},{1},{2},{3},{4})".format(self.south, self.west, self.width_WE, self.width_NS,"10")
def __repr__(self):
__str__()
此外,如果您要将其作为参数传递,您可能不应该明确设置 height
:
...
self.height=10
...
应阅读:
...
self.height=height
...
我需要 return 给出最后一行打印的内容。 在底部,我用值调用了 class。另外,欢迎指点以改进代码。
class Building:
def __init__(self, south, west, width_WE, width_NS, height=10):
# making variables non-local
self.south=int(south)
self.west=int(west)
self.width_WE=int(width_WE)
self.width_NS=int(width_NS)
self.height=height
self.d={}
self.d['north-east']=(south+width_NS,west+width_WE)
self.d['south-east']=(south,west+width_WE)
self.d['south-west']=(south,west)
self.d['north-west']=(south+width_NS,west)
self.wwe=width_WE
self.wns=width_NS
self.height=10
def corner(self): # gives co-ordinates of the corners
print(self.d)
def area (self): # gives area
print(self.wwe*self.wns)
return(self.wwe*self.wns)
def volume(self): #gives volume
print(self.wwe*self.wns*self.height)
def __repr__(self): # I dont know what to call it but answer should be''Building(10, 10, 1, 2, 2)''
print ("Building(%s,%s,%s,%s,%s)"%(self.south, self.west, self.width_WE, self.width_NS,"10"))
#return ("Building(%s,%s,%s,%s,%s)"%(self.south, self.west, self.width_WE, self.width_NS,"10"))
abc = Building(10, 10, 1, 2, 2)
abc.corner()
abc.area()
abc.volume()
改用__str__
:
def __str__(self):
return "Building({0},{1},{2},{3},{4})".format(self.south, self.west, self.width_WE, self.width_NS,"10")
def __repr__(self):
__str__()
此外,如果您要将其作为参数传递,您可能不应该明确设置 height
:
...
self.height=10
...
应阅读:
...
self.height=height
...