Python 无法修复此问题

Python 无法修复此问题,python,class,Python,Class,我正在努力解决这个问题。我犯了一个错误。我不知道该去哪里修理 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): ret

我正在努力解决这个问题。我犯了一个错误。我不知道该去哪里修理

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])
类位置(对象):
定义初始化(self,x,y):
self.x=x
self.y=y
def移动(自助、德尔泰、德尔泰):
返回位置(self.x+deltaX、self.y+deltaY)
def getX(自我):
返回self.x
def getY(自我):
回归自我
def dist_from(自身、其他):
xDist=self.x-other.x
yDist=self.y-other.y
返回(xDist**2+yDist**2)**0.5
定义(自身、其他):
返回(self.x==other.x和self.y==other.y)
定义(自我):
返回“”
班级(对象):
定义初始位置(自身、中心位置):
self.center\u loc=center\u loc
定义(自我):
返回str(自身中心位置)
班级(校园):
“MIT校园是包含帐篷的校园”
定义初始位置(自身、中心位置、帐篷位置=位置(0,0)):
“”“假定中心位置和帐篷位置是位置对象
初始化以位置中心为中心的新校园
在tent_loc''位置有一个帐篷
#你的代码在这里
self.center\u loc=center\u loc
self.tent\u loc=tent\u loc
def添加帐篷(自身、新帐篷位置):
“”“假定新位置是一个位置”
仅当帐篷距离至少为0.5时,才向校园添加新的帐篷位置
远离已经在那里的所有其他帐篷。否则,校园将保持不变。
如果可以添加帐篷,则返回True,否则返回False。”“”
#你的代码在这里
尝试:
self.tent_loc[object]+=1
除:
self.tent_loc[对象]=1
在self.tent\u loc中返回新的\u tent\u loc
def拆除帐篷(自身、帐篷位置):
“”“假定帐篷位置是一个位置。”
将帐篷从校园中移除。
如果帐篷位置没有帐篷,则引发ValueError。
不返回任何内容“”
#你的代码在这里
如果帐篷位置不在self.tent\u loc中:
返回
self.tent\u loc[tent\u loc]=1
如果自身帐篷位置[帐篷位置]<1:
del(自身帐篷位置[帐篷位置])
例如,如果
c=MITCampus(位置(1,2))
则执行以下命令序列:

c.add_帐篷(位置(2,3))
应返回
True

c.add_帐篷(位置(0,0))
应返回
False

c.add_帐篷(位置(2,3))
应返回
False

c.get_tents()
应返回
['','']

我得到的是错误:
名为'MITCampus'的类应该定义一个名为get\u tents的方法。

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

这不起作用:
对象是一个内置的,不能用作序列索引。

找到一个快速的解决方案比解释一切都要快;)

类位置(对象):
定义初始化(self,x,y):
self.x=x
self.y=y
def移动(自助、德尔泰、德尔泰):
返回位置(self.x+deltaX、self.y+deltaY)
def getX(自我):
返回self.x
def getY(自我):
回归自我
def dist_from(自身、其他):
xDist=self.x-other.x
yDist=self.y-other.y
距离=(xDist**2+yDist**2)**0.5
返回区
定义(自身、其他):
返回(self.x==other.x和self.y==other.y)
定义(自我):
返回“”
班级(对象):
定义初始位置(自身、中心位置):
self.center\u loc=center\u loc
定义(自我):
返回str(自身中心位置)
班级(校园):
“MIT校园是包含帐篷的校园”
定义初始位置(自身、中心位置、帐篷位置=位置(0,0)):
“”“假定中心位置和帐篷位置是位置对象
初始化以位置中心为中心的新校园
在tent_loc''位置有一个帐篷
#你的代码在这里
断言isinstance(中心位置)
断言isinstance(帐篷位置)
self.center\u loc=center\u loc
self.tent\u locs=[tent\u loc]
def添加帐篷(自身、新帐篷位置):
“”“假定新位置是一个位置”
仅当帐篷距离至少为0.5时,才向校园添加新的帐篷位置
远离已经在那里的所有其他帐篷。否则,校园将保持不变。
如果可以添加帐篷,则返回True,否则返回False。”“”
#你的代码在这里
断言isinstance(新帐篷位置)
添加=错误
对于自助帐篷位置中的帐篷位置:
如果帐篷位置距离(新帐篷位置)为真
assert not c.add_tent(位置(0,0))35;->Flase
assert not c.add_tent(位置(2,3))35;->False
断言c.get_tents()=['','']
尝试:
c、 拆除帐篷(位置(6,6))
打印“删除失败”
除值错误外:
#预期行为
通过
c、 拆除帐篷(位置(2,3))
断言c.get_tents()=['',]

希望这有帮助!想想我的名誉;)

您需要编写一个名为
get\u tents
的方法,该方法是哪个实体向您发送该错误消息的?例如,当我将代码保存到文件并运行
python x.py
时,我不会收到任何错误消息。我应该使用什么?您需要的任何不是内置代码、关键字或模块的符号;)任何不是内置、关键字或包名称的单词。“schtroumpf”、“foo”、“bar”这些词意义非凡。此外,在将其用作序列索引之前,您应该对其进行初始化,除非您将得到一个NameError异常。您试图如何处理
self.tent_loc[object]=1
行。它没有意义,因为
self.tent_loc
是一个
位置
对象,它不是一个可变序列(类似列表)。这是我在尝试
c.add_tent(位置(2,3))
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
        dist = (xDist ** 2 + yDist ** 2) ** 0.5
        return dist

    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
        assert isinstance(center_loc, Location)
        assert isinstance(tent_loc, Location)
        self.center_loc = center_loc
        self.tent_locs = [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
        assert isinstance(new_tent_loc, Location)
        added = False
        for tent_loc in self.tent_locs:
            if tent_loc.dist_from(new_tent_loc) <= 0.5:
                break
        else:
            self.tent_locs.append(new_tent_loc)
            added = True
        return added

    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
        assert isinstance(tent_loc, Location)
        position = self.tent_locs.index(tent_loc)
        del self.tent_locs[position]

    def get_tents(self):
        return sorted([str(x) for x in [self.center_loc] + self.tent_locs])


c = MITCampus(Location(1, 2))
assert c.add_tent(Location(2, 3))  # -> True
assert not c.add_tent(Location(0, 0))  # -> Flase
assert not c.add_tent(Location(2, 3))  # -> False
assert c.get_tents() == ['<0,0>', '<1,2>', '<2,3>']
try:
    c.remove_tent(Location(6, 6))
    print "removal failed"
except ValueError:
    # Expected behaviour
    pass
c.remove_tent(Location(2, 3))
assert c.get_tents() == ['<0,0>', '<1,2>']