2017-08-01 80 views
-1

我想解决这个问题。我收到错误。我不明白的地方修复无法解决这个

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.

+0

您需要编写一个名为'get_tents'的方法 – quamrana

+1

什么实体向您发送错误消息?例如,当我将代码保存到一个文件并运行'python x.py'时,我没有收到任何错误消息。 –

回答

0
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 

这可不行:object是不能被用作序列索引一个内置。

+0

我应该使用什么? –

+0

您需要的任何符号不是内建的,关键字或模块;) 任何不是内建关键字或包名的字。 “schtroumpf”,“foo”,“bar”,wahtever有意义。另外,除非你得到一个NameError异常,否则你应该在使用它作为序列索引之前对它进行初始化。 – glenfant

+0

你可以给我的代码? –

0

找到一个快速的解决方案是不是解释一切更快;)

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>'] 

希望这有助于!想想我的名声;)