我想解決這個問題。我收到錯誤。我不明白的地方修復無法解決這個
class Location(object):
def __init__(self, x, y):
self.x = x
self.y = y
def move(self, deltaX, deltaY):
return Location(self.x + deltaX, self.y + deltaY)
def getX(self):
return self.x
def getY(self):
return self.y
def dist_from(self, other):
xDist = self.x - other.x
yDist = self.y - other.y
return (xDist ** 2 + yDist ** 2) ** 0.5
def __eq__(self, other):
return (self.x == other.x and self.y == other.y)
def __str__(self):
return '<' + str(self.x) + ',' + str(self.y) + '>'
class Campus(object):
def __init__(self, center_loc):
self.center_loc = center_loc
def __str__(self):
return str(self.center_loc)
class MITCampus(Campus):
""" A MITCampus is a Campus that contains tents """
def __init__(self, center_loc, tent_loc=Location(0, 0)):
""" Assumes center_loc and tent_loc are Location objects
Initializes a new Campus centered at location center_loc
with a tent at location tent_loc """
# Your code here
self.center_loc = center_loc
self.tent_loc = tent_loc
def add_tent(self, new_tent_loc):
""" Assumes new_tent_loc is a Location
Adds new_tent_loc to the campus only if the tent is at least 0.5 distance
away from all other tents already there. Campus is unchanged otherwise.
Returns True if it could add the tent, False otherwise. """
# Your code here
try:
self.tent_loc[object] += 1
except:
self.tent_loc[object] = 1
return new_tent_loc in self.tent_loc
def remove_tent(self, tent_loc):
""" Assumes tent_loc is a Location
Removes tent_loc from the campus.
Raises a ValueError if there is not a tent at tent_loc.
Does not return anything """
# Your code here
if tent_loc not in self.tent_loc:
return
self.tent_loc[tent_loc] -= 1
if self.tent_loc[tent_loc] < 1:
del (self.tent_loc[tent_loc])
例如,如果c = MITCampus(Location(1,2))
然後執行下面的命令序列:
c.add_tent(Location(2,3))
應該返回True
c.add_tent(Location(0,0))
應該返回False
c.add_tent(Location(2,3))
應返回False
c.get_tents()
應該返回['<0,0>', '<1,2>', '<2,3>']
我得到的錯誤:The class named 'MITCampus' should define a method named get_tents.
您需要編寫一個名爲'get_tents'的方法 – quamrana
什麼實體向您發送錯誤消息?例如,當我將代碼保存到一個文件並運行'python x.py'時,我沒有收到任何錯誤消息。 –